-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements DreamMetaObjectIcon, Width(), Height() (#650)
* Implements DreamMetaObjectIcon, Width(), Height() * Cleanup * Clean it up but do it well * Update OpenDreamRuntime/Objects/MetaObjects/DreamMetaObjectIcon.cs Co-authored-by: wixoa <[email protected]> * Update OpenDreamRuntime/Objects/MetaObjects/DreamMetaObjectIcon.cs Co-authored-by: wixoa <[email protected]> * Update OpenDreamRuntime/Objects/MetaObjects/DreamMetaObjectIcon.cs Co-authored-by: wixoa <[email protected]> * de-nullify * fix Co-authored-by: ike709 <[email protected]> Co-authored-by: wixoa <[email protected]>
- Loading branch information
1 parent
323acbb
commit c362445
Showing
7 changed files
with
176 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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 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
140 changes: 140 additions & 0 deletions
140
OpenDreamRuntime/Objects/MetaObjects/DreamMetaObjectIcon.cs
This file contains 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,140 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using OpenDreamRuntime.Procs; | ||
using OpenDreamRuntime.Resources; | ||
using OpenDreamShared.Dream; | ||
using OpenDreamShared.Resources; | ||
|
||
namespace OpenDreamRuntime.Objects.MetaObjects { | ||
sealed class DreamMetaObjectIcon : DreamMetaObjectDatum | ||
{ | ||
[Dependency] private readonly DreamResourceManager _rscMan = default!; | ||
|
||
public enum DreamIconMovingMode : byte | ||
{ | ||
Both = 0, | ||
Movement = 1, | ||
NonMovement = 2, | ||
|
||
} | ||
|
||
public static Dictionary<DreamObject, DreamIconObject> ObjectToDreamIcon = new(); | ||
|
||
public struct DreamIconObject { | ||
// Actual DMI data | ||
public DMIParser.ParsedDMIDescription Description; // TODO Eventually this should probably be removed in favor of just directly storing the data for the subset of the DMI that we actually care about | ||
|
||
// These vars correspond to the args in icon/new() and the resulting /icon obj, not the actual DMI data | ||
public string Icon; | ||
public string? State; // Specific icon_state. Null is all states. | ||
public AtomDirection? Direction; // Specific dir. Null is all dirs. | ||
public byte? Frame; //1-indexed. Specific frame. Null is all frames. | ||
public DreamIconMovingMode Moving; | ||
|
||
public DreamIconObject(DreamResource rsc, DreamValue state, DreamValue dir, DreamValue frame, DreamValue moving) | ||
{ | ||
if (Path.GetExtension(rsc.ResourcePath) != ".dmi") | ||
{ | ||
throw new Exception("Invalid icon file"); | ||
} | ||
|
||
Description = DMIParser.ParseDMI(new MemoryStream(rsc.ResourceData)); | ||
Icon = rsc.ResourcePath; | ||
|
||
// TODO confirm BYOND behavior of invalid args for icon, dir, and frame | ||
|
||
if (state.TryGetValueAsString(out var iconState)) | ||
{ | ||
State = iconState; | ||
} | ||
else | ||
{ | ||
State = null; | ||
} | ||
|
||
if (dir.TryGetValueAsInteger(out var dirVal) && (AtomDirection)dirVal != AtomDirection.None) | ||
{ | ||
Direction = (AtomDirection)dirVal; | ||
} | ||
else | ||
{ | ||
Direction = null; | ||
} | ||
|
||
if (frame.TryGetValueAsInteger(out var frameVal)) | ||
{ | ||
//TODO: Figure out how many frames an icon can have and see if this needs to be bigger than a byte | ||
Frame = Convert.ToByte(frameVal - 1); //1-indexed | ||
} | ||
else | ||
{ | ||
Frame = null; | ||
} | ||
|
||
if (moving != DreamValue.Null) | ||
{ | ||
if (moving.TryGetValueAsInteger(out var movingVal) && movingVal == 0) | ||
{ | ||
Moving = DreamIconMovingMode.NonMovement; | ||
} | ||
else | ||
{ | ||
Moving = DreamIconMovingMode.Movement; | ||
} | ||
} | ||
else | ||
{ | ||
Moving = DreamIconMovingMode.Both; | ||
} | ||
} | ||
} | ||
|
||
public override void OnObjectCreated(DreamObject dreamObject, DreamProcArguments creationArguments) { | ||
base.OnObjectCreated(dreamObject, creationArguments); | ||
|
||
DreamValue icon = creationArguments.GetArgument(0, "icon"); | ||
DreamValue state = creationArguments.GetArgument(1, "icon_state"); | ||
DreamValue dir = creationArguments.GetArgument(2, "dir"); | ||
DreamValue frame = creationArguments.GetArgument(3, "frame"); | ||
DreamValue moving = creationArguments.GetArgument(4, "moving"); | ||
|
||
DreamIconObject dreamIconObject; | ||
|
||
if (icon.TryGetValueAsDreamObjectOfType(DreamPath.Icon, out DreamObject copyFrom)) { | ||
dreamIconObject = ObjectToDreamIcon[copyFrom]; | ||
} else if (icon.TryGetValueAsString(out string fileString)) | ||
{ | ||
var ext = Path.GetExtension(fileString); | ||
switch (ext) // TODO implement other icon file types | ||
{ | ||
case ".dmi": | ||
dreamIconObject = new DreamIconObject(_rscMan.LoadResource(fileString), state, dir, frame, moving); | ||
break; | ||
case ".png": | ||
case ".jpg": | ||
case ".rsi": // RT-specific, not in BYOND | ||
case ".gif": | ||
case ".bmp": | ||
throw new NotImplementedException($"Unimplemented icon type '{ext}'"); | ||
default: | ||
throw new Exception($"Invalid icon file {fileString}"); | ||
} | ||
|
||
} else if (icon.TryGetValueAsDreamResource(out var rsc)) | ||
{ | ||
dreamIconObject = new DreamIconObject(rsc, state, dir, frame, moving); | ||
} else { | ||
throw new Exception("Invalid icon file " + icon); | ||
} | ||
|
||
ObjectToDreamIcon.Add(dreamObject, dreamIconObject); | ||
|
||
} | ||
|
||
public override void OnObjectDeleted(DreamObject dreamObject) { | ||
ObjectToDreamIcon.Remove(dreamObject); | ||
|
||
base.OnObjectDeleted(dreamObject); | ||
} | ||
} | ||
} |
This file contains 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 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 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,23 @@ | ||
using System; | ||
using OpenDreamRuntime.Objects; | ||
using OpenDreamRuntime.Objects.MetaObjects; | ||
|
||
namespace OpenDreamRuntime.Procs.Native { | ||
static class DreamProcNativeIcon { | ||
[DreamProc("Width")] | ||
public static DreamValue NativeProc_Width(DreamObject instance, DreamObject usr, DreamProcArguments arguments) { | ||
DreamMetaObjectIcon.DreamIconObject dreamIconObject = DreamMetaObjectIcon.ObjectToDreamIcon[instance]; | ||
|
||
return new DreamValue(dreamIconObject.Description.Width); | ||
} | ||
|
||
[DreamProc("Height")] | ||
public static DreamValue NativeProc_Height(DreamObject instance, DreamObject usr, DreamProcArguments arguments) { | ||
DreamMetaObjectIcon.DreamIconObject dreamIconObject = DreamMetaObjectIcon.ObjectToDreamIcon[instance]; | ||
|
||
return new DreamValue(dreamIconObject.Description.Height); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains 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