From 3914e084033ff26b18c5024f590ac5ee87b5e391 Mon Sep 17 00:00:00 2001 From: Julia Butenhoff Date: Tue, 19 Jul 2022 09:52:01 -0500 Subject: [PATCH] Implement Save/Load profile - Need testing --- .../ArkOne/ArkOneController.java | 6 +- .../ArkOne/Plugins/GalaxyPlugin.java | 63 ++++++++++++++++++- .../UBFunkeysServer/service/FileService.java | 4 +- .../service/impl/FileServiceImpl.java | 17 ++++- 4 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/icedberries/UBFunkeysServer/ArkOne/ArkOneController.java b/src/main/java/com/icedberries/UBFunkeysServer/ArkOne/ArkOneController.java index 423cd74..e4b9ff0 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/ArkOne/ArkOneController.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/ArkOne/ArkOneController.java @@ -89,8 +89,7 @@ public class ArkOneController implements TcpHandler { // Plugin 7 (Galaxy) case "lpv": - // TODO: FIX THIS METHOD - responses.add(galaxyPlugin.LoadProfileVersion()); + responses.add(galaxyPlugin.LoadProfileVersion(connection)); break; case "vsu": responses.add(galaxyPlugin.VersionStatisticsRequest()); @@ -101,6 +100,9 @@ public class ArkOneController implements TcpHandler { case "spp": responses.add(galaxyPlugin.SaveProfilePart(commandInfo, connection)); break; + case "lp": + responses.add(galaxyPlugin.LoadProfile(connection)); + break; // Plugin 10 (Trunk) diff --git a/src/main/java/com/icedberries/UBFunkeysServer/ArkOne/Plugins/GalaxyPlugin.java b/src/main/java/com/icedberries/UBFunkeysServer/ArkOne/Plugins/GalaxyPlugin.java index 6328bea..0cbdba6 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/ArkOne/Plugins/GalaxyPlugin.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/ArkOne/Plugins/GalaxyPlugin.java @@ -3,9 +3,12 @@ package com.icedberries.UBFunkeysServer.ArkOne.Plugins; import com.icedberries.UBFunkeysServer.ArkOne.ArkOneParser; import com.icedberries.UBFunkeysServer.service.FileService; import javagrinko.spring.tcp.Connection; +import javagrinko.spring.tcp.Server; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Service; +import org.springframework.util.FileCopyUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.xml.sax.InputSource; @@ -16,17 +19,57 @@ import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.TransformerException; import java.io.IOException; +import java.io.InputStreamReader; +import java.io.Reader; import java.io.StringReader; +import java.io.UncheckedIOException; + +import static java.nio.charset.StandardCharsets.UTF_8; @Service public class GalaxyPlugin { + @Autowired + Server server; + @Autowired FileService fileService; - public String LoadProfileVersion() { - //TODO: IMPLEMENT THIS AFTER PROFILE SAVING - return ""; + public String LoadProfileVersion(Connection connection) throws ParserConfigurationException, IOException, + SAXException, TransformerException { + // Start of response + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); + Document resp = dBuilder.newDocument(); + Element rootElement = resp.createElement("h7_0"); + resp.appendChild(rootElement); + + Element subElement = resp.createElement("lpv"); + + // Check if this user has a save + if (fileService.fileExists(server.getConnectedUsers().get(connection.getClientIdentifier()).getUsername() + "/profile")) { + Resource resource = fileService.load(server.getConnectedUsers().get(connection.getClientIdentifier()).getUsername() + "/profile"); + + // Load their save to a string + String content; + try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) { + content = FileCopyUtils.copyToString(reader); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + // Parse the current save + Document save = dBuilder.parse(new InputSource(new StringReader(content))); + save.getDocumentElement().normalize(); + Element rootSave = (Element)save.getFirstChild(); + + String saveID = rootSave.getAttribute("sid"); + subElement.setAttribute("v", saveID); + } + resp.appendChild(subElement); + + // Build response + return ArkOneParser.RemoveXMLTag(resp); } public String VersionStatisticsRequest() { @@ -88,4 +131,18 @@ public class GalaxyPlugin { // Build response return ArkOneParser.RemoveXMLTag(resp); } + + public String LoadProfile(Connection connection) { + Resource resource = fileService.load(server.getConnectedUsers().get(connection.getClientIdentifier()).getUsername() + "/profile"); + + // Load their save to a string + String content; + try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) { + content = FileCopyUtils.copyToString(reader); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + + return "" + content + ""; + } } diff --git a/src/main/java/com/icedberries/UBFunkeysServer/service/FileService.java b/src/main/java/com/icedberries/UBFunkeysServer/service/FileService.java index 60d7d58..62e3854 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/service/FileService.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/service/FileService.java @@ -7,5 +7,7 @@ public interface FileService { void save(MultipartFile file, String subDir); - Resource load(String filename); + Resource load(String path); + + Boolean fileExists(String path); } diff --git a/src/main/java/com/icedberries/UBFunkeysServer/service/impl/FileServiceImpl.java b/src/main/java/com/icedberries/UBFunkeysServer/service/impl/FileServiceImpl.java index fc9a3ee..cae7935 100644 --- a/src/main/java/com/icedberries/UBFunkeysServer/service/impl/FileServiceImpl.java +++ b/src/main/java/com/icedberries/UBFunkeysServer/service/impl/FileServiceImpl.java @@ -36,9 +36,10 @@ public class FileServiceImpl implements FileService { } @Override - public Resource load(String filename) { + public Resource load(String path) { try { - Resource resource = new UrlResource(fileStorageLocation.toUri()); + Path file = fileStorageLocation.resolve(path); + Resource resource = new UrlResource(file.toUri()); if (resource.exists() || resource.isReadable()) { return resource; @@ -49,4 +50,16 @@ public class FileServiceImpl implements FileService { throw new RuntimeException("Error: " + e.getMessage()); } } + + @Override + public Boolean fileExists(String path) { + try { + Path file = fileStorageLocation.resolve(path); + Resource resource = new UrlResource(file.toUri()); + + return resource.exists() || resource.isReadable(); + } catch(MalformedURLException e) { + return false; + } + } }