Skip to content

Commit

Permalink
Fix stack overflow error
Browse files Browse the repository at this point in the history
  • Loading branch information
RedstoneWizard08 committed Mar 21, 2023
1 parent 373ab6e commit 65f2c50
Showing 1 changed file with 40 additions and 22 deletions.
62 changes: 40 additions & 22 deletions common/src/instances/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,42 +101,60 @@ impl Instance {
return instances;
}

pub fn set_default_instances() {
let instances = Instance::load();
pub fn load_no_set() -> Vec<Self> {
let instances;
let instances_path = get_data_dir().join("instances.json");

let ksp1_default = instances
.iter()
.find(|i| i.game == KSPGame::KSP1)
.unwrap()
.clone();
if instances_path.exists() {
let mut file = File::open(instances_path).unwrap();
let mut content = String::new();

let ksp2_default = instances
.iter()
.find(|i| i.game == KSPGame::KSP2)
.unwrap()
.clone();
file.read_to_string(&mut content).unwrap();
instances = serde_json::from_str(&content).unwrap();
} else {
instances = Instance::defaults();

let ksp1_default_json = InstanceJson {
id: ksp1_default.id,
};
Instance::save_all(&instances);
}

let ksp2_default_json = InstanceJson {
id: ksp2_default.id,
};
return instances;
}

let ksp1_instance_json_path = ksp1_default.install_path.join("instance.json");
let ksp2_instance_json_path = ksp2_default.install_path.join("instance.json");
pub fn set_default_instances() {
let ksp1_instance_json_path = find_ksp1_install_dir().join("instance.json");
let ksp2_instance_json_path = find_ksp2_install_dir().join("instance.json");

let ksp1_instance_json = serde_json::to_string(&ksp1_default_json).unwrap();
let ksp2_instance_json = serde_json::to_string(&ksp2_default_json).unwrap();
let instances = Instance::load_no_set();

if !ksp1_instance_json_path.exists() {
let ksp1_default = instances
.iter()
.find(|i| i.game == KSPGame::KSP1)
.unwrap();

let ksp1_default_json = InstanceJson {
id: ksp1_default.id,
};

let ksp1_instance_json = serde_json::to_string(&ksp1_default_json).unwrap();

let mut file = File::create(ksp1_instance_json_path).unwrap();

file.write_all(ksp1_instance_json.as_bytes()).unwrap();
}

if !ksp2_instance_json_path.exists() {
let ksp2_default = instances
.iter()
.find(|i| i.game == KSPGame::KSP2)
.unwrap();

let ksp2_default_json = InstanceJson {
id: ksp2_default.id,
};

let ksp2_instance_json = serde_json::to_string(&ksp2_default_json).unwrap();

let mut file = File::create(ksp2_instance_json_path).unwrap();

file.write_all(ksp2_instance_json.as_bytes()).unwrap();
Expand Down

0 comments on commit 65f2c50

Please sign in to comment.