Skip to content

Commit

Permalink
Fix race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
thejudge156 committed Apr 15, 2024
1 parent beb2717 commit 82bb770
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/main/java/pojlib/api/API_V1.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ public static boolean deleteInstance(MinecraftInstances instances, MinecraftInst
* @return A minecraft instance object
* @throws IOException Throws if download of library or asset fails
*/
public static MinecraftInstances.Instance createNewInstance(Activity activity, String instanceName, String home, boolean useDefaultMods, String minecraftVersion, String modsFolderName) throws IOException {
public static MinecraftInstances.Instance createNewInstance(Activity activity, MinecraftInstances instances, String instanceName, String home, boolean useDefaultMods, String minecraftVersion, String modsFolderName) throws IOException {

if(ignoreInstanceName) {
return InstanceHandler.create(activity, instanceName, home, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, modsFolderName);
return InstanceHandler.create(activity, instances, instanceName, home, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, modsFolderName);
} else if (instanceName.contains("/") || instanceName.contains("!")) {
throw new IOException("You cannot use special characters (!, /, ., etc) when creating instances.");
} else {
return InstanceHandler.create(activity, instanceName, home, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, modsFolderName);
return InstanceHandler.create(activity, instances, instanceName, home, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, modsFolderName);
}
}

Expand Down
31 changes: 11 additions & 20 deletions src/main/java/pojlib/instance/InstanceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ public enum ModLoader {
}

//creates a new instance of a minecraft version, install game + mod loader, stores non login related launch info to json
public static MinecraftInstances.Instance create(Activity activity, String instanceName, String gameDir, boolean useDefaultMods, String minecraftVersion, ModLoader modLoader, String modsFolderName) {
public static MinecraftInstances.Instance create(Activity activity, MinecraftInstances instances, String instanceName, String gameDir, boolean useDefaultMods, String minecraftVersion, ModLoader modLoader, String modsFolderName) {
File instancesFile = new File(gameDir + "/instances.json");
if (instancesFile.exists()) {
MinecraftInstances instances = GsonUtils.jsonFileToObject(instancesFile.getAbsolutePath(), MinecraftInstances.class);
for (MinecraftInstances.Instance instance : instances.instances) {
if (instance.instanceName.equals(instanceName)) {
Logger.getInstance().appendToLog("Instance " + instanceName + " already exists! Using original instance.");
Expand Down Expand Up @@ -98,6 +97,16 @@ public static MinecraftInstances.Instance create(Activity activity, String insta

// Install minecraft
VersionInfo finalModLoaderVersionInfo = modLoaderVersionInfo;

if(instances.instances == null) {
instances.instances = new MinecraftInstances.Instance[0];
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
}

ArrayList<MinecraftInstances.Instance> instances1 = Lists.newArrayList(instances.instances);
instances1.add(instance);
instances.instances = instances1.toArray(new MinecraftInstances.Instance[0]);

new Thread(() -> {
try {
String clientClasspath = Installer.installClient(minecraftVersionInfo, gameDir);
Expand All @@ -113,24 +122,6 @@ public static MinecraftInstances.Instance create(Activity activity, String insta
}
instance.assetIndex = minecraftVersionInfo.assetIndex.id;

MinecraftInstances instances;
try {
instances = GsonUtils.jsonFileToObject(gameDir + "/instances.json", MinecraftInstances.class);
} catch (Exception e) {
instances = new MinecraftInstances();
instances.instances = new MinecraftInstances.Instance[0];
}

if(instances == null) {
instances = new MinecraftInstances();
instances.instances = new MinecraftInstances.Instance[0];
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
}

ArrayList<MinecraftInstances.Instance> instances1 = Lists.newArrayList(instances.instances);
instances1.add(instance);
instances.instances = instances1.toArray(new MinecraftInstances.Instance[0]);

// Write instance to json file
GsonUtils.objectToJsonFile(gameDir + "/instances.json", instances);
instance.updateMods(Constants.MC_DIR, instances);
Expand Down

0 comments on commit 82bb770

Please sign in to comment.