Skip to content
This repository has been archived by the owner on Nov 13, 2022. It is now read-only.

Commit

Permalink
-- Adding HomeCommand for setting custom runtime Leikr Home directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Torbuntu committed Oct 19, 2020
1 parent b558600 commit 6f39ce1
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 26 deletions.
71 changes: 48 additions & 23 deletions core/src/main/java/leikr/GameRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
import org.mini2Dx.core.graphics.Pixmap;
import org.mini2Dx.core.graphics.viewport.FitViewport;

public class GameRuntime extends ScreenBasedGame {
public final class GameRuntime extends ScreenBasedGame {

public final String GAME_IDENTIFIER = "torbuntu.leikr";
public String fileDroppedTitle;
Expand All @@ -58,11 +58,11 @@ public class GameRuntime extends ScreenBasedGame {
private boolean directLaunch;
private String gameName;

private final String programsPath;
private final String basePath;
private final String dataPath;
private final String deployPath;
private final String packagePath;
private String programsPath;
private String basePath;
private String dataPath;
private String deployPath;
private String packagePath;

private final FitViewport viewport;
private AssetManager assetManager;
Expand All @@ -88,30 +88,18 @@ public class GameRuntime extends ScreenBasedGame {

private CustomCursor cursor;

private final CustomSystemProperties customSystemProperties;
private CustomSystemProperties customSystemProperties;

/**
* Creates CustomSystemProperties for detecting launch title.
*
* @param args
*/
public GameRuntime(String[] args) {
String userHome = System.getProperty("user.home");
String leikrHome = System.getenv("LEIKR_HOME");
if (leikrHome != null) {
basePath = leikrHome + "/Leikr/";
programsPath = leikrHome + "/Leikr/Programs/";
dataPath = leikrHome + "/Leikr/Data/";
deployPath = leikrHome + "/Leikr/Deploy/";
packagePath = leikrHome + "/Leikr/Packages/";
Logger.getLogger(GameRuntime.class.getName()).log(Level.INFO, "Using custom Leikr home at: {0}", basePath);
if (System.getenv("LEIKR_HOME") != null) {
customPathVariables();
} else {
basePath = userHome + "/Leikr/";
programsPath = userHome + "/Leikr/Programs/";
dataPath = userHome + "/Leikr/Data/";
deployPath = userHome + "/Leikr/Deploy/";
packagePath = userHome + "/Leikr/Packages/";
Logger.getLogger(GameRuntime.class.getName()).log(Level.INFO, "Using default Leikr home at: {0}", basePath);
defaultPathVariables();
}

System.out.println(programsPath + "\n" + dataPath);
Expand All @@ -130,7 +118,44 @@ public GameRuntime(String[] args) {
}
}

void checkFileSystem() throws IOException {
public void setLeikrHome(String leikrHome) {
try {
basePath = leikrHome + "/Leikr/";
programsPath = leikrHome + "/Leikr/Programs/";
dataPath = leikrHome + "/Leikr/Data/";
deployPath = leikrHome + "/Leikr/Deploy/";
packagePath = leikrHome + "/Leikr/Packages/";

customSystemProperties = new CustomSystemProperties();
checkFileSystem();
Logger.getLogger(GameRuntime.class.getName()).log(Level.INFO, "Using custom Leikr home at: {0}", basePath);

} catch (IOException ex) {
Logger.getLogger(GameRuntime.class.getName()).log(Level.WARNING, "Unable to use custom Leikr home: {0}", basePath);
}
}

private void customPathVariables() {
String leikrHome = System.getenv("LEIKR_HOME");
basePath = leikrHome + "/Leikr/";
programsPath = leikrHome + "/Leikr/Programs/";
dataPath = leikrHome + "/Leikr/Data/";
deployPath = leikrHome + "/Leikr/Deploy/";
packagePath = leikrHome + "/Leikr/Packages/";
Logger.getLogger(GameRuntime.class.getName()).log(Level.INFO, "Using custom Leikr home at: {0}", basePath);
}

private void defaultPathVariables() {
String userHome = System.getProperty("user.home");
basePath = userHome + "/Leikr/";
programsPath = userHome + "/Leikr/Programs/";
dataPath = userHome + "/Leikr/Data/";
deployPath = userHome + "/Leikr/Deploy/";
packagePath = userHome + "/Leikr/Packages/";
Logger.getLogger(GameRuntime.class.getName()).log(Level.INFO, "Using default Leikr home at: {0}", basePath);
}

private void checkFileSystem() throws IOException {
if (!Mdx.files.external(basePath).exists()) {
Mdx.files.external(basePath).mkdirs();
Mdx.files.external(programsPath).mkdirs();
Expand Down
58 changes: 58 additions & 0 deletions core/src/main/java/leikr/commands/HomeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2020 tor.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package leikr.commands;

import java.security.Policy;
import java.util.logging.Level;
import java.util.logging.Logger;
import leikr.GameRuntime;

/**
*
* @author tor
*/
public class HomeCommand implements Command {

private final GameRuntime runtime;

public HomeCommand(GameRuntime runtime){
this.runtime = runtime;
}

@Override
public String execute(String[] args) {
try {
System.setProperty("leikr.home", args[1]);
Policy.getPolicy().refresh();
runtime.setLeikrHome(System.getProperty("leikr.home"));
} catch (Exception ex) {
Logger.getLogger(HomeCommand.class.getName()).log(Level.SEVERE, null, ex);
return "[E] Unable to set LEIKR_HOME: ";
}
return "[I] Set LEIKR_HOME to [" + args[1] + "]";
}

@Override
public String help() {
return "home [path] \nSets the LEIKR_HOME property telling the host where Leikr's home directory should be.";
}

@Override
public String getName() {
return "home";
}

}
4 changes: 3 additions & 1 deletion core/src/main/java/leikr/managers/TerminalManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import leikr.commands.InstallCommand;
import leikr.commands.NewProgramCommand;
import leikr.commands.DeployCommand;
import leikr.commands.HomeCommand;
import leikr.commands.PrintCommand;
import leikr.commands.PrintDirectoryCommand;
import leikr.commands.PrintWorkspaceCommand;
Expand Down Expand Up @@ -120,7 +121,8 @@ public TerminalManager(GameRuntime runtime, EngineLoader engineLoader) {
commandList.put("compile", new CompileCommand(runtime, engineLoader));
commandList.put("get", new GetCommand(runtime));
commandList.put("set", new SetCommand(runtime));

commandList.put("home", new HomeCommand(runtime));

this.runtime = runtime;
}

Expand Down
8 changes: 7 additions & 1 deletion core/src/main/java/leikr/screens/LoadScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ public void render(GameContainer gc, Graphics g) {
viewport.apply(g);
if (!engineGetter.isDone()) {
// logo and loading
g.drawTexture(assetManager.get(runtime.getDataPath()+"Images/leikr-logo.png", Texture.class), 80, 64, 48, 16);
if (assetManager.isLoaded(runtime.getDataPath() + "Images/leikr-logo.png")) {
g.drawTexture(assetManager.get(runtime.getDataPath() + "Images/leikr-logo.png", Texture.class), 80, 64, 48, 16);
} else {
assetManager.load(runtime.getDataPath() + "Images/leikr-logo.png", Texture.class);
assetManager.finishLoading();
}

g.setColor(Colors.WHITE());
g.drawString("Loading", 96, 73);

Expand Down
4 changes: 3 additions & 1 deletion desktop/src/leikr/desktop/DesktopLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public static void main(String[] args) {
if (Arrays.asList(args).contains("insecure")) {
Logger.getLogger(Security.class.getName()).log(Level.WARNING, "Leikr is running without security policy.");
} else if (System.getSecurityManager() == null) {
System.setProperty("leikr.home", System.getenv("LEIKR_HOME"));
if (System.getenv("LEKR_HOME") != null) {
System.setProperty("leikr.home", System.getenv("LEIKR_HOME"));
}
Logger.getLogger(Security.class.getName()).log(Level.INFO, "Setting Leikr security policy.");

System.setProperty("java.security.policy", new File("Sys/mysecurity.policy").getAbsolutePath());
Expand Down

0 comments on commit 6f39ce1

Please sign in to comment.