Skip to content

Commit

Permalink
Separate and reformat KSCSiteManager and KSCLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
dgfl-gh authored and siimav committed Dec 21, 2020
1 parent f258cd4 commit 0387d78
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 204 deletions.
171 changes: 53 additions & 118 deletions Source/KSCLoader.cs
Original file line number Diff line number Diff line change
@@ -1,147 +1,82 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

/******************************************************************************
* Copyright (c) 2014~2016, Justin Bengtson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/

namespace regexKSP
{
// provides a simple method for different classes to load, manage, and read the launch sites.
public class KSCSiteManager
[KSPAddon(KSPAddon.Startup.MainMenu, false)]
public class ScenarioSpawn : MonoBehaviour
{
public List<ConfigNode> Sites;
public string defaultSite = "";
public string lastSite = "";

public KSCSiteManager()
void Start()
{
Sites = new List<ConfigNode>();
ConfigNode KSCSettings = null;
KSCLoader.instance ??= new KSCLoader();
enabled = false;
}
}

foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("KSCSWITCHER"))
{
KSCSettings = node;
}
if (KSCSettings == null)
{
throw new UnityException("KSCSwitcher KSCSWITCHER config node not found!");
}
public class KSCLoader
{
public static KSCLoader instance = null;
public KSCSiteManager Sites = new KSCSiteManager();

if (KSCSettings.HasValue("DefaultSite"))
{
defaultSite = KSCSettings.GetValue("DefaultSite");
}
if (KSCSettings.HasNode("LaunchSites"))
private void OnGameStateCreated(Game game)
{
LastKSC.CreateSettings(game);
if (HighLogic.LoadedScene == GameScenes.SPACECENTER)
{
ConfigNode node = KSCSettings.GetNode("LaunchSites");
ConfigNode[] sites = node.GetNodes("Site");
ProtoScenarioModule m = HighLogic.CurrentGame.scenarios.FirstOrDefault(m => m.moduleName == "LastKSC");

foreach (ConfigNode site in sites)
if (m == null) return;

LastKSC l = (LastKSC)m.Load(ScenarioRunner.Instance);
bool noSite;
if (!string.IsNullOrEmpty(l.lastSite))
{
if (site.HasValue("name"))
// found a site, load it
ConfigNode site = Sites.GetSiteByName(l.lastSite);
if (site == null)
{
ConfigNode pqsCity = site.GetNode("PQSCity");
if (pqsCity == null) { continue; }
if (pqsCity.HasValue("latitude") && pqsCity.HasValue("longitude"))
{
Sites.Add(site);
}
l.lastSite = Sites.defaultSite;
noSite = true;
}
}
}
else
{
Debug.Log("KSCSwitcher No LaunchSites node found!");
}

Debug.Log("KSCSwitcher loaded " + Sites.Count + " launch sites.");
}


public ConfigNode getSiteByName(string name)
{
foreach (ConfigNode site in Sites)
{
if (site.HasValue("name"))
{
if (site.GetValue("name").Equals(name))
else
{
return site;
KSCSwitcher.SetStartingSite(site);
Debug.Log("KSCSwitcher set the launch site to " + l.lastSite);
return;
}
}
}
return null;
}

public SortedList<string, LaunchSite> getSitesGeographicalList()
{
SortedList<string, LaunchSite> siteLocations = new SortedList<string, LaunchSite>();
double lat, lon, dtmp;

foreach (ConfigNode site in KSCLoader.instance.Sites.Sites)
{
ConfigNode pqsCity = site.GetNode("PQSCity");
LaunchSite temp = new LaunchSite();
temp.name = site.GetValue("name");
temp.displayName = site.GetValue("displayName");
double.TryParse(pqsCity.GetValue("latitude"), out lat);
double.TryParse(pqsCity.GetValue("longitude"), out lon);
temp.geographicLocation = new Vector2d(lat, lon);
if (site.HasValue("description"))
{
temp.description = site.GetValue("description");
}
if (site.HasValue("PQSName"))
{
temp.description = site.GetValue("PQSName");
}
else
{
temp.PQSName = "Kerbin";
l.lastSite = Sites.defaultSite;
noSite = true;
}
if (site.HasValue("availableFromUT"))
if (noSite)
{
if (double.TryParse(site.GetValue("availableFromUT"), out dtmp))
if (!string.IsNullOrEmpty(Sites.defaultSite))
{
temp.availableFromUT = dtmp;
}
}
if (site.HasValue("availableUntilUT"))
{
if (double.TryParse(site.GetValue("availableUntilUT"), out dtmp))
{
temp.availableUntilUT = dtmp;
ConfigNode site = Sites.GetSiteByName(Sites.defaultSite);
if (site == null)
{
Debug.LogError("KSCSwitcher found a default site name but could not retrieve the site config: " + Sites.defaultSite);
return;
}
else
{
KSCSwitcher.SetStartingSite(site);
Debug.Log("KSCSwitcher set the initial launch site to " + Sites.defaultSite);
}
}
}
siteLocations.Add(temp.name, temp);
}
}

return siteLocations;
public KSCLoader()
{
GameEvents.onGameStateCreated.Add(OnGameStateCreated);
}
}

}
148 changes: 148 additions & 0 deletions Source/KSCSiteManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
using System.Collections.Generic;
using UnityEngine;

/******************************************************************************
* Copyright (c) 2014~2016, Justin Bengtson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/

namespace regexKSP
{
// provides a simple method for different classes to load, manage, and read the launch sites.
public class KSCSiteManager
{
public List<ConfigNode> Sites;
public string defaultSite = "";
public string lastSite = "";

public KSCSiteManager()
{
Sites = new List<ConfigNode>();
ConfigNode KSCSettings = null;

foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("KSCSWITCHER"))
{
KSCSettings = node;
}
if (KSCSettings == null)
{
throw new UnityException("KSCSwitcher KSCSWITCHER config node not found!");
}

if (KSCSettings.HasValue("DefaultSite"))
{
defaultSite = KSCSettings.GetValue("DefaultSite");
}
if (KSCSettings.HasNode("LaunchSites"))
{
ConfigNode node = KSCSettings.GetNode("LaunchSites");
ConfigNode[] sites = node.GetNodes("Site");

foreach (ConfigNode site in sites)
{
if (site.HasValue("name"))
{
ConfigNode pqsCity = site.GetNode("PQSCity");
if (pqsCity == null) continue;

if (pqsCity.HasValue("latitude") && pqsCity.HasValue("longitude"))
{
Sites.Add(site);
}
}
}
}
else
{
Debug.Log("KSCSwitcher No LaunchSites node found!");
}

Debug.Log("KSCSwitcher loaded " + Sites.Count + " launch sites.");
}

public ConfigNode GetSiteByName(string name)
{
foreach (ConfigNode site in Sites)
{
if (site.HasValue("name"))
{
if (site.GetValue("name").Equals(name))
{
return site;
}
}
}
return null;
}

public SortedList<string, LaunchSite> GetSitesGeographicalList()
{
SortedList<string, LaunchSite> siteLocations = new SortedList<string, LaunchSite>();
double dtmp;

foreach (ConfigNode site in KSCLoader.instance.Sites.Sites)
{
ConfigNode pqsCity = site.GetNode("PQSCity");
LaunchSite temp = new LaunchSite
{
name = site.GetValue("name"),
displayName = site.GetValue("displayName")
};
double.TryParse(pqsCity.GetValue("latitude"), out double lat);
double.TryParse(pqsCity.GetValue("longitude"), out double lon);
temp.geographicLocation = new Vector2d(lat, lon);
if (site.HasValue("description"))
{
temp.description = site.GetValue("description");
}
if (site.HasValue("PQSName"))
{
temp.description = site.GetValue("PQSName");
}
else
{
temp.PQSName = "Kerbin";
}
if (site.HasValue("availableFromUT"))
{
if (double.TryParse(site.GetValue("availableFromUT"), out dtmp))
{
temp.availableFromUT = dtmp;
}
}
if (site.HasValue("availableUntilUT"))
{
if (double.TryParse(site.GetValue("availableUntilUT"), out dtmp))
{
temp.availableUntilUT = dtmp;
}
}
siteLocations.Add(temp.name, temp);
}

return siteLocations;
}
}
}
3 changes: 2 additions & 1 deletion Source/KSCSwitcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="KSCLoader.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="KSCSwitcher.cs" />
<Compile Include="CameraFixer.cs" />
<Compile Include="KSCLoader.cs" />
<Compile Include="KSCSiteManager.cs" />
<Compile Include="LaunchSite.cs" />
<Compile Include="LastKSC.cs" />
</ItemGroup>
Expand Down
Loading

0 comments on commit 0387d78

Please sign in to comment.