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
7 changes: 5 additions & 2 deletions Engine/src/Application.bf
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System;
using System.Collections;
using SteelEngine.Window;
using SteelEngine.Events;
using SteelEngine.Input;
using SteelEngine.ECS;
using SteelEngine.ECS.Systems;
using SteelEngine.ECS.Components;
using System.Collections;
using SteelEngine.Console;

namespace SteelEngine
{
Expand All @@ -21,7 +22,7 @@ namespace SteelEngine
private List<BaseComponent> _componentsToDelete ~ delete _;
private List<EntityId> _entitiesToRemoveFromStore ~ delete _;
private GLFWInputManager _inputManager = new GLFWInputManager() ~ delete _;

private GameConsole _gameConsole = new GameConsole() ~ delete _;

public this()
{
Expand Down Expand Up @@ -101,6 +102,8 @@ namespace SteelEngine
{
Log.AddHandle(Console.Out);

_gameConsole.Initialize(scope String[]("config.cfg"));

_components = new Dictionary<ComponentId, BaseComponent>();
_componentsToDelete = new List<BaseComponent>();
_entitiesToRemoveFromStore = new List<EntityId>();
Expand Down
158 changes: 158 additions & 0 deletions Engine/src/Console/CVar.bf
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using System.Collections;

namespace SteelEngine.Console
{
abstract class CVar
{
protected String _name ~ delete _;
protected String _help ~ delete _;

public StringView Name => _name;
public StringView Help => _help;
public CVarFlags Flags { get; protected set; }

public bool HasFlags(CVarFlags flags) => Flags.HasFlag(flags);
public void AddFlags(CVarFlags flags) => Flags |= flags;
public void RemoveFlags(CVarFlags flags) => Flags &= ~flags;

public abstract Type Type { get; }

public abstract int32 GetValueInt32();
public abstract int64 GetValueInt64();
public abstract float GetValueFloat() ;
public abstract StringView GetValueString(String buffer) ;

public abstract Result<bool> Execute(StringView strArgs, Span<StringView> args);

public virtual bool IsCommand => false;

protected this(StringView name, StringView help, CVarFlags flags)
{
_name = new .(name);
_help = new .(help);
Flags = flags;
}
}

public delegate void OnCVarChange(CVar cvar);

class ConsoleVar<T> : CVar where T : var
{
protected T* _value;

public ref T Value => *_value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit dangerous if the value is null. We should have null protections or require that a CVar be non-null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't be able to register a null CVar. The GameConsole.RegisterVariable takes ref so I don't think there is way to pass null.


public override Type Type => typeof(T);

public override int32 GetValueInt32() { return CVarUtil.GetValueInt32(*_value); }
public override int64 GetValueInt64() { return CVarUtil.GetValueInt64(*_value); }
public override float GetValueFloat() { return CVarUtil.GetValueFloat(*_value); }
public override StringView GetValueString(String buffer)
{
let start = buffer.Length;
(*_value).ToString(buffer);
return .(buffer, start);
}

public override Result<bool> Execute(StringView strArgs, Span<StringView> args)
{
bool changed;
if (args.Length >= 1 && CVarUtil.TryParse(this, args, ref *_value, out changed))
{
return changed;
}

return .Err;
}

public override void ToString(String strBuffer)
{
strBuffer.AppendF("{0} ", Name);
GetValueString(strBuffer);
}

public this(StringView name, StringView help, T* val, CVarFlags flags) : base(name, help, flags)
{
_value = val;
}
}


class EnumConsoleVar<TEnum> : ConsoleVar<TEnum> where TEnum: Enum
{
public override int32 GetValueInt32() { return (*_value); }
public override int64 GetValueInt64() { return (*_value); }
public override float GetValueFloat() { return (int)(*_value); }


public this(StringView name, StringView help, TEnum* val, CVarFlags flags) : base(name, help, val, flags)
{

}
}

public delegate bool OnCmdExecute(CVar cmd, StringView line, Span<StringView> args);
public delegate void OnCmdExecuteNoArgs();
public delegate void OnCmdExecuteLineArgs(StringView line, Span<StringView> args);

class ConsoleCommand<OnExecute> : CVar where OnExecute : Delegate
{
protected OnExecute _onExecute ~ delete _;

public override Type Type => typeof(OnExecute);

public override int32 GetValueInt32() => 0;
public override int64 GetValueInt64() => 0;
public override float GetValueFloat() => 0;
public override StringView GetValueString(String buffer) => default;

public override bool IsCommand => true;

public override void ToString(String strBuffer)
{
strBuffer.AppendF("{0}", Name);
}

public this(StringView name, StringView help, OnExecute onExec, CVarFlags flags) : base(name, help, flags)
{
_onExecute = onExec;
}
}

extension ConsoleCommand<OnExecute> where OnExecute : OnCmdExecute
{
public override Result<bool> Execute(StringView strArgs, Span<StringView> args)
{
if (_onExecute == null)
return .Err;

return _onExecute(this, strArgs, args) ? .Ok(false) : .Err;
}
}

extension ConsoleCommand<OnExecute> where OnExecute : OnCmdExecuteNoArgs
{
public override Result<bool> Execute(StringView strArgs, Span<StringView> args)
{
if (_onExecute == null)
return .Err;

_onExecute();
return false;
}
}

extension ConsoleCommand<OnExecute> where OnExecute : OnCmdExecuteLineArgs
{
public override Result<bool> Execute(StringView strArgs, Span<StringView> args)
{
if (_onExecute == null)
return .Err;

_onExecute(strArgs, args);
return false;
}
}

}
21 changes: 21 additions & 0 deletions Engine/src/Console/CVarFlags.bf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace SteelEngine.Console
{
enum CVarFlags
{
None = 0x0000,
/// Can be executed only when cheats are enabled
Cheat = 0x0001,
/// Indicates that this CVar will change value on registration to match value in configuration file
Config = 0x0002,
/// This flag is set when CVar was present in configuration file
WasInConfig = 0x0004,
/// This flags is set when CVar value is changed after configuration file was loaded
Changed = 0x0008,
/// CVar won't show in console but its value can be changed through code
Hidden = 0x0010,
/// OnChange callback will always be called even when value didn't change
AlwaysOnChange = 0x0020,
/// Disable value check for enum variables
Flags = 0x0040
}
}
Loading