Skip to content
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

Implements DreamMetaObjectIcon, Width(), Height() #650

Merged
merged 8 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 4 additions & 7 deletions DMCompiler/DMStandard/Types/Icon.dm
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/icon
parent_type = /datum
var/icon

New(icon, icon_state, dir, frame, moving)

proc/Blend(icon, function = ICON_ADD, x = 1, y = 1)
set opendream_unimplemented = TRUE
CRASH("/icon.Blend() is not implemented")

proc/Crop(x1, y1, x2, y2)
set opendream_unimplemented = TRUE
CRASH("/icon.Crop() is not implemented")
Expand All @@ -18,14 +19,12 @@
proc/Flip(dir)
set opendream_unimplemented = TRUE
CRASH("/icon.Flip() is not implemented")

proc/GetPixel(x, y, icon_state, dir = 0, frame = 0, moving = -1)
set opendream_unimplemented = TRUE
CRASH("/icon.GetPixel() is not implemented")

proc/Height()
set opendream_unimplemented = TRUE
CRASH("/icon.Height() is not implemented")

proc/IconStates(mode = 0)
return icon_states(src, mode)
Expand Down Expand Up @@ -59,8 +58,6 @@
CRASH("/icon.Turn() is not implemented")

proc/Width()
set opendream_unimplemented = TRUE
CRASH("/icon.Width() is not implemented")

proc/icon(...)
return new /icon(arglist(args))
return new /icon(arglist(args))
1 change: 1 addition & 0 deletions OpenDreamRuntime/DreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ private void SetMetaObjects() {
ObjectTree.SetMetaObject(DreamPath.Turf, new DreamMetaObjectTurf());
ObjectTree.SetMetaObject(DreamPath.Movable, new DreamMetaObjectMovable());
ObjectTree.SetMetaObject(DreamPath.Mob, new DreamMetaObjectMob());
ObjectTree.SetMetaObject(DreamPath.Icon, new DreamMetaObjectIcon());
}

public void SetGlobalNativeProc(NativeProc.HandlerFn func) {
Expand Down
126 changes: 126 additions & 0 deletions OpenDreamRuntime/Objects/MetaObjects/DreamMetaObjectIcon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
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 struct DreamIcon {
public DreamResource Resource;
public string? State;
public AtomDirection Direction;
public int Frame;
public byte? Moving;
public DMIParser.ParsedDMIDescription Description;

public DreamIcon(DreamResource rsc, DreamValue? state, DreamValue? dir, DreamValue? frame, DreamValue? moving)
{
Resource = rsc;
Description = DMIParser.ParseDMI(new MemoryStream(rsc.ResourceData));

// TODO confirm BYOND behavior of invalid types

if (state is not null && state.Value.TryGetValueAsString(out var iconState))
{
State = iconState;
}
else
{
State = null;
}

if (dir is not null && dir.Value.TryGetValueAsInteger(out var dirVal) && (AtomDirection)dirVal != AtomDirection.None)
{
Direction = (AtomDirection)dirVal;
}
else
{
Direction = AtomDirection.South;
}

if (frame is not null && frame.Value.TryGetValueAsInteger(out var frameVal))
{
Frame = frameVal;
}
else
{
Frame = 0;
}

if (moving != DreamValue.Null)
{
if (moving is not null && (!moving.Value.TryGetValueAsInteger(out var movingVal) || movingVal != 0))
{
Moving = 1;
}
else
{
Moving = 0;
}
}
else
{
Moving = null;
}
}
}

public static Dictionary<DreamObject, DreamIcon> ObjectToDreamIcon = new();

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");

DreamIcon dreamIcon;

if (icon.TryGetValueAsDreamObjectOfType(DreamPath.Icon, out DreamObject copyFrom)) {
dreamIcon = ObjectToDreamIcon[copyFrom];
} else if (icon.TryGetValueAsString(out string fileString))
{
var ext = Path.GetExtension(fileString);
switch (ext) // TODO implement other icon file types
{
case ".dmi":
dreamIcon = new DreamIcon(_rscMan.LoadResource(fileString), state, dir, frame, moving);
Stream dmiStream = new MemoryStream(dreamIcon.Resource.ResourceData);
dreamIcon.Description = DMIParser.ParseDMI(dmiStream);
dreamObject.SetVariableValue("icon", new DreamValue(dreamIcon.Resource.ResourcePath));
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))
{
dreamIcon = new DreamIcon(rsc, state, dir, frame, moving);
dreamObject.SetVariableValue("icon", new DreamValue(dreamIcon.Resource.ResourcePath));
} else {
throw new Exception("Invalid icon file " + icon);
}

ObjectToDreamIcon.Add(dreamObject, dreamIcon);

}

public override void OnObjectDeleted(DreamObject dreamObject) {
ObjectToDreamIcon.Remove(dreamObject);

base.OnObjectDeleted(dreamObject);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using OpenDreamShared.Dream;

namespace OpenDreamRuntime.Objects.MetaObjects {
class DreamMetaObjectRegex : DreamMetaObjectDatum {
sealed class DreamMetaObjectRegex : DreamMetaObjectDatum {
public struct DreamRegex {
public Regex Regex;
public bool IsGlobal;
Expand Down
4 changes: 4 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ public static void SetupNativeProcs(DreamObjectTree objectTree) {
regex.SetNativeProc(DreamProcNativeRegex.NativeProc_Find);
regex.SetNativeProc(DreamProcNativeRegex.NativeProc_Replace);

DreamObjectDefinition icon = objectTree.GetObjectDefinition(DreamPath.Icon);
icon.SetNativeProc(DreamProcNativeIcon.NativeProc_Width);
icon.SetNativeProc(DreamProcNativeIcon.NativeProc_Height);

//DreamObjectDefinition savefile = objectTree.GetObjectDefinitionFromPath(DreamPath.Savefile);
//savefile.SetNativeProc(DreamProcNativeSavefile.NativeProc_Flush);

Expand Down
23 changes: 23 additions & 0 deletions OpenDreamRuntime/Procs/Native/DreamProcNativeIcon.cs
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.DreamIcon dreamIcon = DreamMetaObjectIcon.ObjectToDreamIcon[instance];

return new DreamValue(dreamIcon.Description.Width);
}

[DreamProc("Height")]
public static DreamValue NativeProc_Height(DreamObject instance, DreamObject usr, DreamProcArguments arguments) {
DreamMetaObjectIcon.DreamIcon dreamIcon = DreamMetaObjectIcon.ObjectToDreamIcon[instance];

return new DreamValue(dreamIcon.Description.Height);
}


}
}