Skip to content

Commit

Permalink
Removed unneccessary InstanceOverrideDisplay class
Browse files Browse the repository at this point in the history
Inlined InstanceOverrideDisplay to Executables.Instance
Renamed Game.OS.Extensions' WriteSingle -> WriteAppend
Renamed Game.OS.Extensions' Write -> WriteLine
Addresses #29
  • Loading branch information
Spartan322 committed Apr 19, 2020
1 parent 0bc54e8 commit 12ea319
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 49 deletions.
63 changes: 18 additions & 45 deletions Pathfinder/Executable/ExecutableInstance.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Collections.Generic;
using Hacknet;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pathfinder.Executable
{
public class Instance : ExeModule
public class Instance : ExeModule, MainDisplayOverrideEXE
{
private Dictionary<string, object> keyToObject = new Dictionary<string, object>();

Expand All @@ -23,6 +22,7 @@ public Instance(Rectangle loc, OS os, List<string> args, FileEntry executionFile
IdentifierName = Interface.Identifier;
needsProxyAccess = Interface.NeedsProxyAccess;
ramCost = Interface.RamCost;
isInterfaceDisplayOverride = Interface is IMainDisplayOverride;
Interface.OnConstruction(this);
}

Expand All @@ -32,34 +32,35 @@ public Instance(Rectangle loc, OS os, List<string> args, FileEntry executionFile
List<string> args,
Rectangle loc)
{
if (exeInterface is IMainDisplayOverride)
return new InstanceOverrideDisplay(loc, os, args, executionFile, exeInterface);
return new Instance(loc, os, args, executionFile, exeInterface);
}

public static Instance CreateInstance(Interface exeInterface,
FileEntry executionFile,
OS os,
List<string> args) =>
CreateInstance(exeInterface, executionFile, os, args, Rectangle.Empty);
CreateInstance(exeInterface, executionFile, os, args, Rectangle.Empty);

private readonly bool isInterfaceDisplayOverride;
private bool isOverrideable = true;
public bool DisplayOverrideIsActive
{
get => isInterfaceDisplayOverride && isOverrideable && ((IMainDisplayOverride)Interface).IsOverrideActive(this);
set => isOverrideable = value;
}

public object this[string key]
{
get
{
object o;
if (keyToObject.TryGetValue(key, out o))
if (keyToObject.TryGetValue(key, out object o))
return o;
return null;
}
set
{
keyToObject[key] = value;
}
set => keyToObject[key] = value;
}

public object GetInstanceData(string key) => this[key];
public T GetInstanceData<T>(string key) => (T)GetInstanceData(key);
public T GetInstanceData<T>(string key) => (T)this[key];

public bool SetInstanceData(string key, object val)
{
Expand Down Expand Up @@ -121,37 +122,9 @@ public override void PostDrawStep()
{
base.PostDrawStep();
Interface.PostDraw(this);
}

public class InstanceOverrideDisplay : Instance, MainDisplayOverrideEXE
{
private bool isOverrideable = true;

public bool DisplayOverrideIsActive
{
get
{
return isOverrideable && (Interface as IMainDisplayOverride).IsOverrideActive(this);
}
set
{
isOverrideable = value;
}
}

public InstanceOverrideDisplay(Rectangle loc,
OS os,
List<string> arguments,
FileEntry executionFile,
Interface exeInterface)
: base(loc, os, arguments, executionFile, exeInterface)
{
if (!(exeInterface is IMainDisplayOverride))
throw new ArgumentException("exeInterface must be derived from IMainDisplayOverride");
}

public void RenderMainDisplay(Rectangle dest, SpriteBatch sb) =>
(Interface as IMainDisplayOverride).DrawMain(this, dest, sb);
}
}

public void RenderMainDisplay(Rectangle dest, SpriteBatch sb) =>
((IMainDisplayOverride)Interface).DrawMain(this, dest, sb);
}
}
8 changes: 4 additions & 4 deletions Pathfinder/Game/OS/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,22 +119,22 @@ public static void KillAllExecutableModules(this Hacknet.OS os, bool shouldWrite
public static Hacknet.Computer GetCurrentComputer(this Hacknet.OS os) => Utility.GetCurrentComputer(os);

/// <summary>
/// Writes the formatted string to OS terminal.
/// Writes the formatted string as a line to OS terminal.
/// </summary>
/// <param name="os">The OS.</param>
/// <param name="write">The formatted string to write.</param>
public static Hacknet.OS Write(this Hacknet.OS os, string write, params object[] args)
public static Hacknet.OS WriteLine(this Hacknet.OS os, string write, params object[] args)
{
os.write(args == null || args.Length < 1 ? write : string.Format(write, args));
return os;
}

/// <summary>
/// Writes the formatted string directly to OS terminal. Less safe then <see cref="Write"/>
/// Appends the formatted string to OS terminal. Less safe then <see cref="WriteLine"/>
/// </summary>
/// <param name="os">The OS.</param>
/// <param name="write">The formatted string to write.</param>
public static Hacknet.OS WriteSingle(this Hacknet.OS os, string write, params object[] args)
public static Hacknet.OS WriteAppend(this Hacknet.OS os, string write, params object[] args)
{
os.writeSingle(args == null || args.Length < 1 ? write : string.Format(write, args));
return os;
Expand Down

0 comments on commit 12ea319

Please sign in to comment.