Skip to content

Commit

Permalink
I didn't keep track of what changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
fingerboxes committed May 2, 2015
1 parent 65924f3 commit 111eb46
Show file tree
Hide file tree
Showing 6 changed files with 681 additions and 0 deletions.
215 changes: 215 additions & 0 deletions Source/HoloDeck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Alexander Taylor
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

using UnityEngine;

using HoloDeck.SceneModules;

namespace HoloDeck
{
// We want to load in all relevant game scenes, and be applied to all games.
[KSPScenario(ScenarioCreationOptions.AddToAllGames, new GameScenes[] { GameScenes.SPACECENTER, GameScenes.EDITOR, GameScenes.FLIGHT, GameScenes.TRACKSTATION })]
class HoloDeck : ScenarioModule
{
// Tag for marking debug logs with
private const string TAG = "HoloDeck.HoloDeck";

// This class is a singleton, as much as Unity will allow.
// There is probably a better way to do this in a Unity-like way,
// But I don't know it.
public static HoloDeck instance;

// This is a flag for marking a save as 'dirty'. Any flag with this flag
// that enters SPACECENTER, EDITOR, or TRACKSTATION will be immediately reset
[KSPField(isPersistant = true)]
public bool SimulationActive = false;

// This is our entry point
void Start()
{
// update the singleton
instance = this;

// Reload to pre-sim if we are in the wrong scene.
if (HighLogic.LoadedScene != GameScenes.FLIGHT)
{
HoloDeck.Deactivate(GameScenes.SPACECENTER);
}

// Deploy scene-specific modules, for GUI hijacking and similar logic
switch (HighLogic.LoadedScene)
{
case GameScenes.FLIGHT:
gameObject.AddComponent<FlightModule>();
break;
case GameScenes.EDITOR:
gameObject.AddComponent<EditorModule>();
break;
}

// If the sim is active, display the tell-tale
SimulationNotification(SimulationActive);
}

// This is called when the script is destroyed.
// This is honestly probably not necessary, but better safe than sorry
void Destroy()
{
SimulationNotification(false);
instance = null;
}

// Activates the Simulation. Returns the success of the activation.
public static bool Activate()
{
// for recording save status, not sure what this string actually is tbh
string save = null;

// Make sure the instance actually exists. I can't imagine this ever failing, but NREs are bad.
if (instance != null)
{
// We create the pre-sim save.
save = GamePersistence.SaveGame("HoloDeckRevert", HighLogic.SaveFolder, SaveMode.OVERWRITE);

// Mark the existing save as dirty.
HoloDeck.instance.SimulationActive = true;

// Record the scene we are coming from
HoloDeckShelter.lastScene = HighLogic.LoadedScene;

if (HoloDeckShelter.lastScene == GameScenes.EDITOR)
{
HoloDeck.OnLeavingEditor(EditorDriver.editorFacility, EditorLogic.fetch.launchSiteName);
}

// Start the tell-tale
HoloDeck.instance.SimulationNotification(true);
}

return save != null ? true : false;
}

// Deactivates the simulation. Success is destructive to the plugin state,
// so... no return value
public static void Deactivate(GameScenes targetScene)
{
// This method only does something if the sim is active.
if (HoloDeck.instance.SimulationActive)
{
// Weird bug can be intorduced by how KSP keeps around KSPAddons until it decides to destroy
// them. We need to preempt this so extraneous behavior isn't observed
FlightModule[] flightModules = GameObject.FindObjectsOfType(typeof(FlightModule)) as FlightModule[];
EditorModule[] editorModules = GameObject.FindObjectsOfType(typeof(EditorModule)) as EditorModule[];

foreach (FlightModule flightModule in flightModules)
{
DestroyImmediate(flightModule);
}

foreach (EditorModule editorModule in editorModules)
{
DestroyImmediate(editorModule);
}

// Ok, here is where this is tricky. We can't just directly load the save, we need to
// load the save into a new Game object, re-save that object into the default persistence,
// and then change the scene. Weird shit, right? This is actually how the vanilla quickload
// works!
Game newGame = GamePersistence.LoadGame("HoloDeckRevert", HighLogic.SaveFolder, true, false);
GamePersistence.SaveGame(newGame, "persistent", HighLogic.SaveFolder, SaveMode.OVERWRITE);
newGame.startScene = targetScene;

// This has to be before... newGame.Start()
if (targetScene == GameScenes.EDITOR)
{
newGame.editorFacility = HoloDeckShelter.lastEditor;
}

newGame.Start();
HoloDeck.instance.SimulationActive = false;

// ... And this has to be after. <3 KSP
if (targetScene == GameScenes.EDITOR)
{
EditorDriver.StartupBehaviour = EditorDriver.StartupBehaviours.LOAD_FROM_CACHE;
ShipConstruction.ShipConfig = HoloDeckShelter.lastShip;
}
}
}

// This method should be called before activating the simulation directly from an editor, and allows
// QOL improvements (like returning to that editor correctly, and automatically clearing the launch site)
// Either of these values can be null, if you want to do that for some reason
public static void OnLeavingEditor(EditorFacility facility, string launchSiteName)
{
// clear the launchpad.
if (launchSiteName != null)
{
List<ProtoVessel> junkAtLaunchSite = ShipConstruction.FindVesselsLandedAt(HighLogic.CurrentGame.flightState, launchSiteName);

foreach (ProtoVessel pv in junkAtLaunchSite)
{
HighLogic.CurrentGame.flightState.protoVessels.Remove(pv);
}
}

if (facility != EditorFacility.None)
{
HoloDeckShelter.lastEditor = facility;
}

HoloDeckShelter.lastShip = ShipConstruction.ShipConfig;
}

// This is in here instead of GUI, because this isn't an 'implementation detail'
// If some other mod uses sim mode for some reason, I still want this displayed.
private void SimulationNotification(bool state)
{
if (HighLogic.LoadedScene == GameScenes.FLIGHT)
{
switch (state)
{
case true:
InvokeRepeating("DoSimulationNotification", 0.1f, 1.0f);
break;

case false:
CancelInvoke("DoSimulationNotification");
break;
}
}
}

private void DoSimulationNotification()
{
ScreenMessages.PostScreenMessage("Simulation Active", 1.0f, ScreenMessageStyle.LOWER_CENTER);
}
}
}
65 changes: 65 additions & 0 deletions Source/HoloDeck.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8C5539DD-396B-4084-892E-8F1901E51584}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HoloDeck</RootNamespace>
<AssemblyName>HoloDeck</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
<ItemGroup>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\..\..\..\..\Games\Kerbal Space Program\KSP_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp-firstpass">
<HintPath>..\..\..\..\..\..\..\Games\Kerbal Space Program\KSP_Data\Managed\Assembly-CSharp-firstpass.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\..\..\..\..\..\..\Games\Kerbal Space Program\KSP_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="SceneModules\EditorModule.cs" />
<Compile Include="SceneModules\FlightModule.cs" />
<Compile Include="HoloDeck.cs" />
<Compile Include="HoloDeckShelter.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>xcopy /Y $(TargetPath) $(TargetDir)..\..\..\GameData\Fingerboxes\HoloDeck\Plugins\</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
28 changes: 28 additions & 0 deletions Source/HoloDeck.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2013
VisualStudioVersion = 12.0.31101.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HoloDeck", "HoloDeck.csproj", "{8C5539DD-396B-4084-892E-8F1901E51584}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FingerboxLib", "..\..\FingerboxLib\Source\FingerboxLib.csproj", "{6AE0BA91-9473-43E6-980D-C7B04CCAEB45}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8C5539DD-396B-4084-892E-8F1901E51584}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8C5539DD-396B-4084-892E-8F1901E51584}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8C5539DD-396B-4084-892E-8F1901E51584}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8C5539DD-396B-4084-892E-8F1901E51584}.Release|Any CPU.Build.0 = Release|Any CPU
{6AE0BA91-9473-43E6-980D-C7B04CCAEB45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6AE0BA91-9473-43E6-980D-C7B04CCAEB45}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AE0BA91-9473-43E6-980D-C7B04CCAEB45}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AE0BA91-9473-43E6-980D-C7B04CCAEB45}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
24 changes: 24 additions & 0 deletions Source/HoloDeckShelter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace HoloDeck.SceneModules
{
// This class is a place to stick data that will not be destroyed by HoloDeck.Deactivate().
[KSPAddon(KSPAddon.Startup.Instantly, true)]
class HoloDeckShelter : MonoBehaviour
{
// Editor stuff
public static EditorFacility lastEditor = EditorFacility.None;
public static ConfigNode lastShip = null;

public static GameScenes lastScene;

void Awake()
{
DontDestroyOnLoad(this);
}
}
}
Loading

0 comments on commit 111eb46

Please sign in to comment.