Added storage classes - work in progress II

This commit is contained in:
Robert Vokac 2024-08-10 13:36:22 +02:00
parent 31914ff6af
commit 99d0b95b29
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
12 changed files with 480 additions and 206 deletions

View File

@ -37,6 +37,8 @@ import com.openeggbert.mods.Mod;
import com.openeggbert.mods.ModIdentification;
import com.openeggbert.screens.GameSpaceListScreen;
import com.openeggbert.screens.InitScreen;
import com.openeggbert.storage.Storage;
import com.openeggbert.storage.StorageImplementationLoader;
import com.openeggbert.utils.OpenEggbertDisplayMode;
import java.util.ArrayList;
import java.util.List;
@ -66,6 +68,7 @@ public class OpenEggbertGame extends Game {
private AssetsTxt assets;
private ConfigDef configDef;
private OpenEggbertDisplayMode openEggbertDisplayMode = OpenEggbertDisplayMode.WINDOW;
private Storage storage;
public OpenEggbertGame() {
this(null, null);
@ -80,6 +83,13 @@ public class OpenEggbertGame extends Game {
this.currentDirectory = currentDirectory;
}
public Storage getStorage() {
if(storage == null) {
this.storage = StorageImplementationLoader.getStorage();
}
return storage;
}
@Override
public void create() {

View File

@ -63,6 +63,7 @@ public abstract class AbstractOpenEggbertScreen extends ScreenAdapter {
}
private void loadBackgroundTextureIfNeeded() {
if(true) return;//todo
if (getBackgroundFileName().isEmpty()) {
return;
}

View File

@ -72,6 +72,8 @@ public class GameSpaceListScreen extends AbstractOpenEggbertScreen {
Preferences prefs = Gdx.app.getPreferences("My Preferences");
prefs.putString("test", "abc");
prefs.flush();
game.getStorage().mkdir("modes");
game.getStorage().mkdir("game_spaces");
}
@Override
@ -175,6 +177,7 @@ public class GameSpaceListScreen extends AbstractOpenEggbertScreen {
@Override
public void renderOpenEggbertScreen(float delta) {
Gdx.app.log(getClass().getName(), game.getStorage().debug());
timeSeconds += Gdx.graphics.getRawDeltaTime();
if (timeSeconds > 60) {

View File

@ -89,5 +89,7 @@ public interface Storage {
public boolean isdir(String name);
public String debug();
public void flush();
}

View File

@ -120,5 +120,10 @@ public abstract class DesktopAndroidStorage implements Storage {
@Override
public String debug() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}
@Override
public void flush() {
//nothing to do
}
}

View File

@ -60,11 +60,17 @@ public class MapStorage implements Storage {
return workingDirectory + (workingDirectory.equals("/") ? "" : SLASH) + path;
}
private static final String TWO_DOTS = "..";
@Override
public String cd(String path) {
String absolutePath = convertToAbsolutePathIfNeeded(path);
if(path.equals(TWO_DOTS)) {
getParentPath(workingDirectory);
}
if (!exists(path)) {
final String msg = "Path does not exist: " + path;
logError(msg);
@ -324,4 +330,9 @@ public class MapStorage implements Storage {
return sb.toString();
}
@Override
public void flush() {
map.flush();
}
}

View File

@ -28,7 +28,6 @@ import com.openeggbert.entity.common.GameFileType;
import com.openeggbert.entity.common.OpenEggbertException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

View File

@ -1,203 +0,0 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.lwjgl3.debugging;
import com.openeggbert.storage.Storage;
import com.openeggbert.storage.map.MemoryStorage;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.Scanner;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
*
* @author robertvokac
*/
public class StorageCommandWrapper {
private StorageCommandWrapper(String userIn, String hostnameIn, Storage storageIn) {
this.user = userIn;
this.hostname = hostnameIn;
this.storage = storageIn;
}
private String user;
private String hostname;
private Storage storage;
// @AllArgsConstructor
// @Data
// class StorageCommandWrapperResult {
//
// private String output;
// private boolean error;
// }
//
// public StorageCommandWrapperResult execute(String commandWithArguments) {
// return null;
// }
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
MemoryStorage memoryStorage = new MemoryStorage();
while (true) {
System.out.print("player@openegggbert:" + memoryStorage.pwd() + "$ ");
String argument = scanner.nextLine();
String[] arguments = argument.split(" ");
int argumentCount = arguments.length - 1;
//System.out.println("argumentCount=" + argumentCount);
Optional<String> argument1 = Optional.ofNullable(argumentCount >= 1 ? arguments[1] : null);
Optional<String> argument2 = Optional.ofNullable(argumentCount >= 2 ? arguments[2] : null);
String command = arguments.length == 0 ? "" : arguments[0];
switch (command) {
case "whoami":
print("player");break;
case "hostname":
print("openegggbert");break;
case "uname":
if (argumentCount == 0) {
print("LinuxBashCommandLinePartialEmulation");
} else {
if (argument1.get().equals("-a")) {
print("LinuxBashCommandLinePartialEmulation openeggbert 0.0.0 (" + LocalDateTime.now().toString().replace("T", " ").substring(0, 10) + ")");
} else {
print("LinuxBashCommandLinePartialEmulation");
}
}
break;
case "exit":
return;
case "":
continue;
case "ls":
memoryStorage
.ls()
.stream()
.map(l->{
String[] a = l.split("/");return a[a.length-1];
}
)
.forEach(l -> print(l));
break;
case "pwd":
print(memoryStorage.pwd());
break;
case "depth":
print(memoryStorage.depth());
break;
case "mkdir":
if (checkArgumentCount(1, argumentCount)) {
String result = memoryStorage.mkdir(argument1.get());
if (result.isEmpty()) {
System.out.println("New directory was successfully created");
} else {
System.err.println("Creating new directory failed: " + result);
}
}
break;
case "touch":
if (checkArgumentCount(1, argumentCount)) {
String result = memoryStorage.touch(argument1.get());
if (result.isEmpty()) {
System.out.println("New file was successfully created");
} else {
System.err.println("Creating new directory failed: " + result);
}
}
break;
case "readtext":
if (checkArgumentCount(1, argumentCount)) {
String result = memoryStorage.readtext(argument1.get());
if (result != null) {
System.out.println("Text file was successfully loaded");
System.out.println(result);
} else {
System.err.println("Loading text file failed:");
}
}
break;
case "savetext":
if (checkArgumentCount(2, argumentCount)) {
String result = memoryStorage.savetext(argument1.get(), argument2.get());
if (result.isEmpty()) {
System.out.println("Text file was successfully saved");
} else {
System.err.println("Saving text file failed: " + result);
}
}
break;
case "cd":
String result = argument1.isEmpty() ? memoryStorage.cd() : memoryStorage.cd(argument1.get());
if (result.isEmpty()) {
System.out.println("Changing working directory was successfully created");
} else {
System.err.println("Changing working directory failed: " + result);
}
break;
case "debug":
print(memoryStorage.debug());
break;
default: {
System.err.println("Unsupported command: " + command);
}
}
}
}
private static void print(int msg) {
print(String.valueOf(msg));
}
private static void print(String msg) {
System.out.println(msg);
}
private static void printError(String msg) {
System.err.println(msg);
}
private static boolean checkArgumentCount(int wantedCount, int currentCount) {
// System.out.println("wantedCount=" + wantedCount);
// System.out.println("currentCount=" + currentCount);
boolean b = wantedCount < currentCount;
// System.out.println("b=" + b);
if (currentCount < wantedCount) {
printError("Wanted argument count is: " + wantedCount + ", but the current count of arguments is: " + currentCount);
// System.out.println("return false");
return false;
}
// System.out.println("return true");
return true;
}
}

View File

@ -0,0 +1,63 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.lwjgl3.debugging.storage;
import java.util.function.Function;
/**
*
* @author robertvokac
*/
public class BaseCommandLine implements StorageCommand {
private StorageCommandLine storageCommandLine = null;
private String name;
private final Function<String, StorageCommandResult> function;
public BaseCommandLine(
StorageCommandLine storageCommandLineIn, String nameIn, Function<String, StorageCommandResult> functionIn
) {
setStorageCommandLine(storageCommandLineIn);
this.name = nameIn;
this.function = functionIn;
}
@Override
public final void setStorageCommandLine(StorageCommandLine storageCommandLineIn) {
storageCommandLine = storageCommandLineIn;
}
@Override
public final StorageCommandLine getStorageCommandLine() {
return storageCommandLine;
}
@Override
public String getName() {
return name;
}
@Override
public StorageCommandResult execute(String commandWithArguments) {
return function.apply(commandWithArguments);
}
}

View File

@ -0,0 +1,38 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.lwjgl3.debugging.storage;
/**
*
* @author robertvokac
*/
public interface StorageCommand {
public String getName();
StorageCommandResult execute(String commandWithArguments);
StorageCommandLine getStorageCommandLine();
void setStorageCommandLine(StorageCommandLine storageCommandLine);
static StorageCommandResult emptyNewResult() {
return new StorageCommandResult();
}
}

View File

@ -0,0 +1,277 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.lwjgl3.debugging.storage;
import com.openeggbert.storage.Storage;
import com.openeggbert.storage.map.MemoryStorage;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
*
* @author robertvokac
*/
public class StorageCommandLine {
private long startNanoTime = System.nanoTime();
private String extractArgument(String arguments, int argumentIndex) {
String[] array = arguments.split(" ");
if(argumentIndex > (array.length - 1)) {
return "";
}
return array[argumentIndex];
}
private StorageCommandLine(String userIn, String hostnameIn, Storage storageIn) {
this.user = userIn;
this.hostname = hostnameIn;
this.storage = storageIn;
addCommand("whoami", arguments -> modifyResult(result -> result.setOutput(user)));
addCommand("uptime", arguments -> modifyResult(result
-> result.setOutput(
LocalDateTime.now().toString().replace("T", " ").substring(10, 19) + " up "
+ (System.nanoTime() - startNanoTime) / 1000000000l / 60l
+ " minutes"
+ ", 1 user"
)));
addCommand("hostname", arguments -> modifyResult(result -> result.setOutput(hostname)));
addCommand("uname", arguments -> modifyResult(result -> result.setOutput(
"LinuxBashCommandLinePartialEmulation"
+
((extractArgument(arguments, 0).equals("-a")) ?
(hostname + " 0.0.0 ("
+
LocalDateTime.now().toString().replace("T", " ").substring(0, 10) + ")"
)
: "")
)));
// 12:31:18 up 2:10, 1 user
commands.keySet().stream().map(k -> commands.get(k)).forEach(c -> c.setStorageCommandLine(this));
}
private StorageCommandResult modifyResult(Consumer<StorageCommandResult> consumer) {
StorageCommandResult result = StorageCommand.emptyNewResult();
consumer.accept(result);
return result;
}
private void addCommand(String nameIn, Function<String, StorageCommandResult> functionIn) {
StorageCommand storageCommand = new BaseCommandLine(this, nameIn, functionIn);
commands.put(storageCommand.getName(), storageCommand);
}
private String user;
private String hostname;
private Storage storage;
private boolean exited = false;
private final Map<String, StorageCommand> commands = new HashMap<>();
public String getUser() {
return user;
}
public String getHostname() {
return hostname;
}
public Storage getStorage() {
return storage;
}
public boolean isExited() {
return exited;
}
public StorageCommandResult execute(String commandWithArguments) {
String[] arguments = commandWithArguments.split(" ");
String command = arguments.length == 0 ? "" : arguments[0];
StorageCommand storageCommand = commands.get(command);
if (storageCommand != null) {
return storageCommand.execute(commandWithArguments.substring(command.length()));
}
int argumentCount = arguments.length - 1;
//System.out.println("argumentCount=" + argumentCount);
Optional<String> argument1 = Optional.ofNullable(argumentCount >= 1 ? arguments[1] : null);
Optional<String> argument2 = Optional.ofNullable(argumentCount >= 2 ? arguments[2] : null);
StorageCommandResult finalResult = new StorageCommandResult();
switch (command) {
case "exit":
exited = true;
finalResult.setOutput("Exited");
break;
case "":
break;
case "ls":
String output = storage
.ls()
.stream()
.map(l -> {
String[] a = l.split("/");
return a[a.length - 1];
}
).collect(Collectors.joining("\n"));
finalResult.setOutput(output);
break;
case "pwd":
finalResult.setOutput(storage.pwd());
break;
case "depth":
finalResult.setOutput(storage.depth());
break;
case "mkdir":
if (checkArgumentCount(1, argumentCount)) {
String r = storage.mkdir(argument1.get());
if (r.isEmpty()) {
finalResult.setOutput("New directory was successfully created");
} else {
finalResult.setErrorOutput("Creating new directory failed: " + r);
}
}
break;
case "touch":
if (checkArgumentCount(1, argumentCount)) {
String result = storage.touch(argument1.get());
if (result.isEmpty()) {
finalResult.setOutput("New file was successfully created");
} else {
finalResult.setErrorOutput("Creating new directory failed: " + result);
}
}
break;
case "readtext":
if (checkArgumentCount(1, argumentCount)) {
String result = storage.readtext(argument1.get());
if (result != null) {
finalResult.setOutput("Text file was successfully loaded" + "\n\n" + result);
} else {
finalResult.setErrorOutput("Loading text file failed:");
}
}
break;
case "savetext":
if (checkArgumentCount(2, argumentCount)) {
String result = storage.savetext(argument1.get(), argument2.get());
if (result.isEmpty()) {
finalResult.setOutput("Text file was successfully saved");
} else {
finalResult.setErrorOutput("Saving text file failed: " + result);
}
}
break;
case "cd":
String r = argument1.isEmpty() ? storage.cd() : storage.cd(argument1.get());
if (r.isEmpty()) {
finalResult.setOutput("Changing working directory was successfully created");
} else {
finalResult.setErrorOutput("Changing working directory failed: " + r);
}
break;
case "debug":
finalResult.setOutput(storage.debug());
break;
default: {
finalResult.setErrorOutput("Unsupported command: " + command);
}
}
return finalResult;
}
public static void main(String[] args) {
MemoryStorage memoryStorage = new MemoryStorage();
StorageCommandLine storageCommandWrapper = new StorageCommandLine("player", "openegggbert", memoryStorage);
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("player@openegggbert:" + memoryStorage.pwd() + "$ ");
String argument = scanner.nextLine();
StorageCommandResult result = storageCommandWrapper.execute(argument);
if (result.isError()) {
printError(result.getOutput());
} else {
print(result.getOutput());
}
if (storageCommandWrapper.isExited()) {
break;
}
}
}
private static void print(int msg) {
print(String.valueOf(msg));
}
private static void print(String msg) {
System.out.println(msg);
}
private static void printError(String msg) {
System.err.println(msg);
}
private static boolean checkArgumentCount(int wantedCount, int currentCount) {
// System.out.println("wantedCount=" + wantedCount);
// System.out.println("currentCount=" + currentCount);
boolean b = wantedCount < currentCount;
// System.out.println("b=" + b);
if (currentCount < wantedCount) {
printError("Wanted argument count is: " + wantedCount + ", but the current count of arguments is: " + currentCount);
// System.out.println("return false");
return false;
}
// System.out.println("return true");
return true;
}
}

View File

@ -0,0 +1,68 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.lwjgl3.debugging.storage;
/**
*
* @author robertvokac
*/
public class StorageCommandResult {
public StorageCommandResult() {
this("");
}
public StorageCommandResult(String output) {
this(output, false);
}
public StorageCommandResult(String output, boolean error) {
this.output = output;
this.error = error;
}
public String getOutput() {
return output;
}
public boolean isError() {
return error;
}
public void setOutput(int output) {
setOutput(String.valueOf(output));
}
public void setErrorOutput(String output) {
this.output = output;
setError(true);
}
public void setOutput(String output) {
this.output = output;
}
public void setError(boolean error) {
this.error = error;
}
private String output;
private boolean error;
}