-
Notifications
You must be signed in to change notification settings - Fork 2
In-game console #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Fusioon
wants to merge
1
commit into
Beef-Community-Project:master
Choose a base branch
from
Fusioon:in-game-console
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
In-game console #33
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
| 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; | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.RegisterVariabletakes ref so I don't think there is way to pass null.