Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templates #13

Merged
merged 6 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import eu.smoothcloud.api.INode;
import eu.smoothcloud.node.configuration.*;
import eu.smoothcloud.node.console.JLineConsole;
import eu.smoothcloud.node.template.TemplateManager;
import eu.smoothcloud.util.converter.CloudList;
import lombok.Getter;

Expand All @@ -33,6 +34,7 @@ public static void main(String[] args) {
private TemplatesConfiguration templatesConfiguration;
private MessageConfiguration messageConfiguration;
private JLineConsole console;
private TemplateManager templateManager;

public SmoothCloudNode() {
this.threads = 2;
Expand All @@ -42,11 +44,12 @@ public SmoothCloudNode() {
this.templatesConfiguration = new TemplatesConfiguration(new CloudList<>(String.class).convertToArray());
this.templatesConfiguration.saveToFile("storage", "templates.toml");
}
this.templateManager = new TemplateManager(this.templatesConfiguration, this.console);
this.startConsole();
}

private void startConsole() {
this.console = new JLineConsole();
this.console = new JLineConsole(this.templateManager);
this.initializeConsole();
this.console.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import eu.smoothcloud.node.console.modes.DefaultMode;
import eu.smoothcloud.node.console.modes.Mode;
import eu.smoothcloud.node.console.modes.SetupMode;
import eu.smoothcloud.node.template.TemplateManager;
import eu.smoothcloud.util.console.ConsoleColor;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -45,10 +46,11 @@ public class JLineConsole {
private boolean isRunning;
private boolean isPaused;

private final TemplateManager templateManager;
private Mode currentMode;

@SneakyThrows
public JLineConsole() {
public JLineConsole(TemplateManager templateManager) {
this.terminal = TerminalBuilder.builder()
.system(true)
.encoding(StandardCharsets.UTF_8)
Expand All @@ -65,7 +67,8 @@ public JLineConsole() {
this.reader.setPrompt(coloredPrefix.toAnsi());
this.isRunning = true;
this.isPaused = false;
this.currentMode = new DefaultMode(this);
this.templateManager = templateManager;
this.currentMode = new DefaultMode(this, templateManager);
this.clear();
this.sendWelcomeMessage();
}
Expand Down Expand Up @@ -114,7 +117,7 @@ public void start() {
public void switchMode(String modeName) {
switch (modeName.toLowerCase()) {
case "setup" -> this.currentMode = new SetupMode(this);
case "default" -> this.currentMode = new DefaultMode(this);
case "default" -> this.currentMode = new DefaultMode(this, this.templateManager);
default -> this.print("Unknown mode: " + modeName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
package eu.smoothcloud.node.console.modes;

import eu.smoothcloud.node.console.JLineConsole;
import eu.smoothcloud.node.template.TemplateManager;
import eu.smoothcloud.util.console.ConsoleColor;

public class DefaultMode extends Mode {
private final JLineConsole console;
private final TemplateManager templateManager;

public DefaultMode(JLineConsole console) {
public DefaultMode(JLineConsole console, TemplateManager templateManager) {
this.console = console;
this.templateManager = templateManager;
}

@Override
Expand All @@ -45,6 +48,10 @@ public void handleCommand(String command, String[] args) {
this.console.print(" &binfo version &7- &fDisplays the current version of the cloud.");
this.console.print(" &binfo group <group> &7- &fDisplays information about the group. ");
this.console.print(" &binfo service <service> &7- &fDisplays information about the service. ");
this.console.print(" &bcreate template <templateName> &7- &fCreate a Template. ");
this.console.print(" &brename template <templateName> <newTemplateName> &7- &fRename a Template. ");
this.console.print(" &bremove template <templateName> &7- &fRemove a Template. ");
this.console.print(" &blist templates &7- &fRemove a Template. ");
this.console.print("&7-------------------------------&bHelp&7-------------------------------");
}
case "info" -> {
Expand All @@ -70,6 +77,69 @@ public void handleCommand(String command, String[] args) {
}
}
}
case "create" -> {
switch (args[0].toLowerCase()) {
case "template" -> {
switch (this.templateManager.addTemplate(args[1])) {
case 0 -> this.console.print("&fYour Template with the Name: &b" + args[1] + " &fis created!");
case 1 -> this.console.print("[FF3333]The Entered Template Name: &b" + args[1] + " [FF3333]is to short!");
case 2 -> this.console.print("[FF3333]The Entered Template Name: &b" + args[1] + " [FF3333]contains forbidden special character!");
case 3 -> this.console.print("[FF3333]The Entered Template Name: &b" + args[1] + " [FF3333]already exists!");
}
}
default -> {
this.console.print("&fUsage:");
this.console.print("&7- &bcreate template <templateName>");
}
}
}
case "rename" -> {
switch (args[0].toLowerCase()) {
case "template" -> {
switch (this.templateManager.renameTemplate(args[1], args[2])) {
case 0 -> this.console.print("&fYour Template with the Name: &b" + args[1] + " &fis renamed to &b" + args[2] + " &f!");
case 1 -> this.console.print("[FF3333]The Entered NewTemplate Name: &b" + args[2] + " [FF3333]is to short!");
case 2 -> this.console.print("[FF3333]The Entered NewTemplate Name: &b" + args[2] + " [FF3333]contains forbidden special character!");
case 3 -> this.console.print("[FF3333]The Entered Template Name: &b" + args[1] + " [FF3333]don't exists!");
case 4 -> this.console.print("[FF3333]The Renaming of this Template failed!");
}
}
default -> {
this.console.print("&fUsage:");
this.console.print("&7- &brename template <templateName> <newTemplateName>");
}
}
}
case "list" -> {
switch (args[0].toLowerCase()) {
case "templates" -> {
this.console.print("&7-------------------------------&bTemplates&7-------------------------------");
this.templateManager.getTemplates().forEach((tempName, tempPath) -> {
this.console.print("&7Name: &b" + tempName + " &7-> &b" + tempPath);
});
this.console.print("&7-------------------------------&bTemplates&7-------------------------------");
}
default -> {
this.console.print("&fUsage:");
this.console.print("&7- &list templates");
}
}
}
case "remove" -> {
switch (args[0].toLowerCase()) {
case "template" -> {
if (this.templateManager.removeTemplate(args[1])) {
this.console.print("&fYour Template with the Name: &b" + args[1] + " &fis removed!");
} else {
this.console.print("[FF3333]The Entered Template Name: &b" + args[1] + " [FF3333]don't exists!");
}
}
default -> {
this.console.print("&fUsage:");
this.console.print("&7- &bremove template <templateName>");
}
}
}
case "clear" -> {
this.console.clear();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package eu.smoothcloud.node.template;

import eu.smoothcloud.node.configuration.TemplatesConfiguration;
import eu.smoothcloud.node.console.JLineConsole;
import eu.smoothcloud.util.charset.CharsetUtil;
import eu.smoothcloud.util.converter.CloudList;
import lombok.Getter;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.HashMap;
import java.util.Map;

public class TemplateManager {
private final JLineConsole console;
private final String DEFAULT_PATH = "templates/";
private final String[] DEFAULT_TEMPLATES = {
"every/", "every_lobby/", "every_proxy/", "every_server/"
};
@Getter
private Map<String, Path> templates;
private TemplatesConfiguration configuration;


public TemplateManager(TemplatesConfiguration configuration, JLineConsole console) {
this.console = console;
this.configuration = configuration;
this.templates = new HashMap<>();
for (String template : configuration.getTemplates()) {
this.templates.put(template, Path.of(DEFAULT_PATH + template));
}
File temp_dir = new File(DEFAULT_PATH);
if(!temp_dir.exists() || !temp_dir.isDirectory()) {
temp_dir.mkdirs();
temp_dir.setWritable(true, false);
}
for (String def_temp : DEFAULT_TEMPLATES) {
File def_temp_dir = new File(DEFAULT_PATH + def_temp);
if(!def_temp_dir.exists()) {
def_temp_dir.mkdirs();
def_temp_dir.setWritable(true, false);
}
}
}

private void saveTemplates() {
CloudList<String> templates = new CloudList<>(String.class);
this.templates.forEach((name, path) -> templates.add(name));
this.configuration.setTemplates(templates.convertToArray());
this.configuration.saveToFile("storage", "templates.toml");
}

public Path getTemplate(String templateName) {
return this.templates.get(templateName);
}

public int addTemplate(String templateName) {
if(templateName.length() <= 2) return 1;
if(!CharsetUtil.isValidInput(templateName)) return 2;
if(this.templates.containsKey(templateName)) return 3;
templates.put(templateName, Path.of(DEFAULT_PATH + templateName + "/"));
File temp_dir = new File(this.templates.get(templateName).toUri());
if(!temp_dir.exists()) {
temp_dir.mkdirs();
temp_dir.setWritable(true, false);
}
saveTemplates();
return 0;
}

public int renameTemplate(String templateName, String newTemplateName) {
try {
if (newTemplateName.length() <= 2) return 1;
if (!CharsetUtil.isValidInput(newTemplateName)) return 2;
if (!this.templates.containsKey(templateName)) return 3;
Path existingTemp = this.templates.get(templateName);
Path newTemp = Path.of(DEFAULT_PATH + newTemplateName);
Files.move(
existingTemp,
newTemp,
StandardCopyOption.ATOMIC_MOVE
);
this.templates.remove(templateName, existingTemp);
this.templates.put(newTemplateName, newTemp);
saveTemplates();
return 0;
} catch (IOException e) {
return 4;
}
}

public boolean removeTemplate(String templateName) {
if(!this.templates.containsKey(templateName)) return false;
Path path = this.templates.get(templateName);
this.templates.remove(templateName, path);
File temp_dir = new File(path.toUri());
temp_dir.delete();
saveTemplates();
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eu.smoothcloud.util.charset;

public class CharsetUtil {
public static boolean isValidInput(String input) {
String charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
for (int i = 0; i < input.length(); i++) {
if (charset.indexOf(input.charAt(i)) == -1) {
return false;
}
}
return true;
}
}
Loading