Skip to content
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/SpecializedIndustryZones/Filepaths.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.IO;
using Colossal.PSI.Environment;

namespace SpecializedIndustryZones;

readonly struct Filepaths
{
public static readonly string ZoneFilePath = Path.Combine(EnvPath.kUserDataPath, "ModsData", "SpecializedZones", "SpecializedZones.json");
}
2 changes: 1 addition & 1 deletion src/SpecializedIndustryZones/SpecializedZoneSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ public class SpecializedZoneFilterSpec
public bool? RequireStored { get; set; }

public bool? RequireSold { get; set; }
}
}
887 changes: 887 additions & 0 deletions src/SpecializedIndustryZones/SpecializedZoneSpecSourceExtra.cs

Large diffs are not rendered by default.

74 changes: 61 additions & 13 deletions src/SpecializedIndustryZones/SpecializedZoningSystem.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
using Colossal.Logging;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Colossal.Logging;
using Colossal.PSI.Environment;
using Game;
using Game.Economy;
Expand All @@ -7,11 +12,6 @@
using Game.UI.InGame;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace SpecializedIndustryZones;

Expand Down Expand Up @@ -76,7 +76,26 @@ protected override void OnUpdate()

if (!File.Exists(ZoneFilePath))
{
// Load extra specialized zones, once
var json_list = new List<string>
{
SpecializedZoneSpecSourceExtra.officeByEducation,
// SpecializedZoneSpecSourceExtra.commercialByEducation,
SpecializedZoneSpecSourceExtra.commercialByModel,
SpecializedZoneSpecSourceExtra.industryByEducation
};
var defaultSpecs = new SpecializedZoneSpecFile();
foreach (var json in json_list)
{
var specs = LoadZoneJson(json);
if (specs != null)
{
foreach (KeyValuePair<string, SpecializedZoneSpec> zone in specs.Zones)
{
defaultSpecs.Zones[zone.Key] = zone.Value;
}
}
}
foreach (var (specID, spec) in GenerateDefaultSpecs())
{
defaultSpecs.Zones[specID] = spec;
Expand All @@ -96,12 +115,30 @@ protected override void OnUpdate()
_lastFileModifiedTimestamp = lastModified;
_log.InfoFormat("Reloading specialized zones from file '{0}'.", ZoneFilePath);
var sw = Stopwatch.StartNew();


var numLoaded = UpdateFromZoneFile();

sw.Stop();
_log.InfoFormat("{0} specialized zones reloaded in {1} ms.", numLoaded, sw.ElapsedMilliseconds);
}

private int UpdateFromZoneFile()
{
var specs = LoadZoneFile();
return UpdateFromSpecs(specs);
}
private int UpdateFromJson(string json)
{
var specs = LoadZoneJson(json);
return UpdateFromSpecs(specs);
}

private int UpdateFromSpecs(SpecializedZoneSpecFile? specs)
{
if (specs == null)
{
_log.Warn("No specs loaded, cannot provision zones.");
return;
return 0;
}

var numLoaded = 0;
Expand Down Expand Up @@ -173,12 +210,10 @@ protected override void OnUpdate()
continue;
}
}

sw.Stop();
_log.InfoFormat("{0} specialized zones reloaded in {1} ms.", numLoaded, sw.ElapsedMilliseconds);
return numLoaded;
}

private static readonly string ZoneFilePath = Path.Combine(EnvPath.kUserDataPath, "ModsData", "SpecializedZones", "SpecializedZones.json");
private static readonly string ZoneFilePath = Filepaths.ZoneFilePath;

private SpecializedZoneSpecFile? LoadZoneFile()
{
Expand All @@ -190,7 +225,7 @@ protected override void OnUpdate()
try
{
var json = File.ReadAllText(ZoneFilePath);
return JsonConvert.DeserializeObject<SpecializedZoneSpecFile>(json, JsonSettings);
return LoadZoneJson(json);
}
catch (Exception e)
{
Expand All @@ -199,6 +234,19 @@ protected override void OnUpdate()
}
}

private SpecializedZoneSpecFile? LoadZoneJson(String json)
{
try
{
return JsonConvert.DeserializeObject<SpecializedZoneSpecFile>(json, JsonSettings);
}
catch (Exception e)
{
Mod.log.Error(e, "Failed to load specialized zones from JSON.");
return null;
}
}

private void SaveZoneFile(SpecializedZoneSpecFile specs)
{
try
Expand Down