Skip to content

Commit a800319

Browse files
author
hamstar0
committed
v2.0.0.4 - Finished "dynamic object" fallback hologram loading
* Reverted TypeDef nonsense back into plain int Type * Finished "dynamic object" fallback hologram loading
1 parent 266b4f1 commit a800319

15 files changed

+173
-115
lines changed

Emitters/Definitions/BaseEmitterDefinition.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Dynamic;
23
using Terraria;
34
using HamstarHelpers.Classes.Errors;
45

@@ -20,6 +21,8 @@ public abstract class BaseEmitterDefinition {
2021

2122
////////////////
2223

24+
public abstract void ReadDynamic( ExpandoObject obj );
25+
2326
public abstract void Read( BinaryReader reader );
2427

2528
public abstract void Write( BinaryWriter writer );

Emitters/Definitions/EmitterDefinition_IO.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
using System.IO;
1+
using System;
2+
using System.Dynamic;
3+
using System.IO;
24
using Microsoft.Xna.Framework;
35

46

57
namespace Emitters.Definitions {
68
public partial class EmitterDefinition : BaseEmitterDefinition {
9+
public override void ReadDynamic( ExpandoObject obj ) {
10+
throw new NotImplementedException( "No dynamic object support." );
11+
}
12+
713
public override void Read( BinaryReader reader ) {
814
this.IsGoreMode = (bool)reader.ReadBoolean();
915
this.Type = (int)reader.ReadUInt16();

Emitters/Definitions/HologramDefinition.cs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Terraria;
55
using Terraria.ModLoader.Config;
66
using HamstarHelpers.Classes.Errors;
7+
using HamstarHelpers.Helpers.Debug;
8+
using Terraria.ModLoader;
79

810

911
namespace Emitters.Definitions {
@@ -21,8 +23,25 @@ public enum HologramShaderMode {
2123

2224

2325

24-
2526
public partial class HologramDefinition : BaseEmitterDefinition {
27+
public static bool IsBadType( HologramMode mode, int type ) {
28+
switch( mode ) {
29+
case HologramMode.NPC:
30+
if( type < Main.npcTexture.Length ) { return false; }
31+
return NPCLoader.GetNPC( type ) == null;
32+
case HologramMode.Item:
33+
if( type < Main.itemTexture.Length ) { return false; }
34+
return ItemLoader.GetItem( type ) == null;
35+
case HologramMode.Projectile:
36+
if( type < Main.projectileTexture.Length ) { return false; }
37+
return ProjectileLoader.GetProjectile( type ) == null;
38+
default:
39+
throw new ModHelpersException( "Invalid hologram type" );
40+
}
41+
}
42+
43+
////
44+
2645
public static Texture2D GetTexture( HologramMode mode, int type ) {
2746
switch( mode ) {
2847
case HologramMode.NPC:
@@ -53,28 +72,36 @@ public static int GetFrameCount( HologramMode mode, int type ) {
5372

5473
////////////////
5574

56-
/*[JsonIgnore]
57-
public EntityDefinition TypeDef {
58-
get => HologramDefinition.GetTypeDef( this.Mode, (string)TempTypeDef );
59-
}
60-
[JsonProperty( "TypeDef" )]
61-
private JObject TempTypeDef { set; get; }*/
62-
6375
public HologramMode Mode { get; set; }
64-
public EntityDefinition TypeDef { get; set; }
76+
77+
public int Type { get; set; }
78+
6579
public float Scale { get; set; }
80+
6681
public Color Color { get; set; }
82+
6783
public byte Alpha { get; set; }
84+
6885
public int Direction { get; set; }
86+
6987
public float Rotation { get; set; }
88+
7089
public int OffsetX { get; set; }
90+
7191
public int OffsetY { get; set; }
92+
7293
public int FrameStart { get; set; }
94+
7395
public int FrameEnd { get; set; }
96+
7497
public int FrameRateTicks { get; set; }
98+
7599
public bool WorldLighting { get; set; }
100+
76101
public HologramShaderMode ShaderMode { get; set; }
102+
77103
public float ShaderTime { get; set; }
104+
78105
public int ShaderType { get; set; }
79106

80107
////
@@ -83,24 +110,6 @@ public EntityDefinition TypeDef {
83110

84111
////
85112

86-
[Obsolete( "use TypeDef", true )]
87-
public int Type {
88-
//get => this.TypeDef.Type;
89-
set {
90-
switch( this.Mode ) {
91-
case HologramMode.NPC:
92-
this.TypeDef = new NPCDefinition( value );
93-
break;
94-
case HologramMode.Item:
95-
this.TypeDef = new ItemDefinition( value );
96-
break;
97-
case HologramMode.Projectile:
98-
this.TypeDef = new ProjectileDefinition( value );
99-
break;
100-
}
101-
}
102-
}
103-
104113
[Obsolete( "use ShaderMode", true )]
105114
public bool CrtEffect {
106115
get => this.ShaderMode == HologramShaderMode.Custom;
@@ -122,7 +131,7 @@ public HologramDefinition() { }
122131

123132
public HologramDefinition( HologramDefinition copy ) {
124133
this.Mode = copy.Mode;
125-
this.TypeDef = copy.TypeDef;
134+
this.Type = copy.Type;
126135
this.Scale = copy.Scale;
127136
this.Color = copy.Color;
128137
this.Alpha = copy.Alpha;
@@ -145,7 +154,7 @@ public HologramDefinition( HologramDefinition copy ) {
145154

146155
public HologramDefinition(
147156
HologramMode mode,
148-
EntityDefinition typeDef,
157+
int type,
149158
float scale,
150159
Color color,
151160
byte alpha,
@@ -162,7 +171,7 @@ public HologramDefinition(
162171
int shaderType,
163172
bool isActivated ) {
164173
this.Mode = mode;
165-
this.TypeDef = typeDef;
174+
this.Type = type;
166175
this.Scale = scale;
167176
this.Color = color;
168177
this.Alpha = alpha;
@@ -190,7 +199,7 @@ private void AnimateCurrentFrame() {
190199
return;
191200
}
192201

193-
int frameCount = HologramDefinition.GetFrameCount( this.Mode, this.TypeDef.Type );
202+
int frameCount = HologramDefinition.GetFrameCount( this.Mode, this.Type );
194203

195204
this.CurrentFrame++;
196205
this.CurrentFrameElapsedTicks = 0;

Emitters/Definitions/HologramDefinition_Animate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public bool AnimateHologram( Vector2 worldPos, bool isUI ) {
88
if( !this.IsActivated ) {
99
return false;
1010
}
11-
if( this.TypeDef == null ) {
11+
if( HologramDefinition.IsBadType(this.Mode, this.Type) ) {
1212
return false;
1313
}
1414

Emitters/Definitions/HologramDefinition_Draw.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ public void Draw( SpriteBatch sb, int tileX, int tileY, bool isOnScreen ) {
2727
public void DrawHologram( SpriteBatch sb, Vector2 wldPos, bool isUI ) {
2828
switch( this.Mode ) {
2929
case HologramMode.NPC:
30-
Main.instance.LoadNPC( this.TypeDef.Type );
30+
Main.instance.LoadNPC( this.Type );
3131
break;
3232
case HologramMode.Projectile:
33-
Main.instance.LoadProjectile( this.TypeDef.Type );
33+
Main.instance.LoadProjectile( this.Type );
3434
break;
3535
}
3636

37-
Texture2D tex = HologramDefinition.GetTexture( this.Mode, this.TypeDef.Type );
38-
var frameCount = HologramDefinition.GetFrameCount( this.Mode, this.TypeDef.Type );
37+
Texture2D tex = HologramDefinition.GetTexture( this.Mode, this.Type );
38+
var frameCount = HologramDefinition.GetFrameCount( this.Mode, this.Type );
3939
var frameHeight = tex.Height / frameCount;
4040

4141

@@ -122,7 +122,7 @@ public Effect CustomEffectsBegin( Texture2D tex ) {
122122
fx.Parameters["RandValue"].SetValue( Main.rand.NextFloat() );
123123
fx.Parameters["CyclePercent"].SetValue( cyclePerc );
124124
fx.Parameters["Frame"].SetValue( (float)this.CurrentFrame );
125-
fx.Parameters["FrameMax"].SetValue( (float)Main.npcFrameCount[this.TypeDef.Type] );
125+
fx.Parameters["FrameMax"].SetValue( (float)Main.npcFrameCount[this.Type] );
126126
fx.Parameters["UserColor"].SetValue( color.ToVector4() );
127127
fx.Parameters["WaveScale"].SetValue( (float)color.A / 255f ); // TODO ?
128128

Emitters/Definitions/HologramDefinition_IO.cs

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,66 @@
11
using System;
2-
using System.Collections;
32
using System.Collections.Generic;
43
using System.IO;
54
using System.Dynamic;
65
using Microsoft.Xna.Framework;
7-
using HamstarHelpers.Classes.Errors;
8-
using Terraria.ModLoader.Config;
96
using Newtonsoft.Json;
7+
using HamstarHelpers.Classes.Errors;
8+
using HamstarHelpers.Helpers.Debug;
109

1110

1211
namespace Emitters.Definitions {
1312
public partial class HologramDefinition : BaseEmitterDefinition {
14-
public void Read( dynamic obj ) {
15-
ExpandoObject eo = JsonConvert.DeserializeObject( JsonConvert.SerializeObject(obj) );
16-
var eod = (IDictionary<string, object>)eo;
13+
public override void ReadDynamic( ExpandoObject obj ) {
14+
var objDict = (IDictionary<string, object>)obj;
15+
16+
if( objDict.ContainsKey("Mode") ) {
17+
this.Mode = (HologramMode)(long)objDict["Mode"];
18+
} else {
19+
this.Mode = HologramMode.NPC;
20+
}
1721

18-
if( eod.ContainsKey("Mode") ) {
22+
this.Type = (int)(long)objDict["Type"];
23+
this.Scale = (float)(double)objDict["Scale"];
24+
this.Color = JsonConvert.DeserializeObject<Color>( "\""+objDict["Color"]+"\"" );
25+
this.Alpha = (byte)(long)objDict["Alpha"];
26+
this.Direction = (int)(long)objDict["Direction"];
27+
this.Rotation = (float)(double)objDict["Rotation"];
28+
this.OffsetX = (int)(long)objDict["OffsetX"];
29+
this.OffsetY = (int)(long)objDict["OffsetY"];
30+
this.FrameStart = (int)(long)objDict["FrameStart"];
31+
this.FrameEnd = (int)(long)objDict["FrameEnd"];
32+
this.FrameRateTicks = (int)(long)objDict["FrameRateTicks"];
33+
this.WorldLighting = (bool)objDict["WorldLighting"];
1934

35+
if( objDict.ContainsKey("ShaderMode") ) {
36+
this.ShaderMode = (HologramShaderMode)(long)objDict["ShaderMode"];
37+
this.ShaderTime = (float)(double)objDict["ShaderTime"];
38+
this.ShaderType = (int)(long)objDict["ShaderType"];
39+
} else if( objDict.ContainsKey("CrtEffect") ) {
40+
this.ShaderMode = (bool)objDict["CrtEffect"]
41+
? HologramShaderMode.None
42+
: HologramShaderMode.Custom;
43+
this.ShaderTime = 1f;
44+
this.ShaderType = 0;
45+
} else {
46+
this.ShaderMode = HologramShaderMode.None;
47+
this.ShaderTime = 1f;
48+
this.ShaderType = 0;
2049
}
21-
this.Mode = (HologramMode)obj.Mode;
22-
this.TypeDef = NPCDefinition.FromString( rawType );
23-
this.Scale = (float)reader.ReadSingle();
24-
this.Color = new Color(
25-
(byte)reader.ReadByte(),
26-
(byte)reader.ReadByte(),
27-
(byte)reader.ReadByte()
28-
);
29-
this.Alpha = (byte)reader.ReadByte();
30-
this.Direction = (int)reader.ReadUInt16();
31-
this.Rotation = (float)reader.ReadSingle();
32-
this.OffsetX = (int)reader.ReadUInt16();
33-
this.OffsetY = (int)reader.ReadUInt16();
34-
this.FrameStart = (int)reader.ReadUInt16();
35-
this.FrameEnd = (int)reader.ReadUInt16();
36-
this.FrameRateTicks = (int)reader.ReadUInt16();
37-
this.WorldLighting = (bool)reader.ReadBoolean();
38-
this.ShaderMode = (HologramShaderMode)reader.ReadUInt16();
39-
this.ShaderTime = (float)reader.ReadSingle();
40-
this.ShaderType = (int)reader.ReadUInt16();
41-
this.IsActivated = (bool)reader.ReadBoolean();
50+
51+
this.IsActivated = (bool)objDict["IsActivated"];
52+
53+
//Color color = this.Color;
54+
//color.A = this.Alpha;
55+
//this.Color = color;
4256
}
4357

4458
////
4559

4660
public override void Read( BinaryReader reader ) {
4761
this.Mode = (HologramMode)reader.ReadUInt16();
4862

49-
string rawType = reader.ReadString();
50-
51-
switch( this.Mode ) {
52-
case HologramMode.NPC:
53-
this.TypeDef = NPCDefinition.FromString( rawType );
54-
break;
55-
case HologramMode.Item:
56-
this.TypeDef = NPCDefinition.FromString( rawType );
57-
break;
58-
case HologramMode.Projectile:
59-
this.TypeDef = NPCDefinition.FromString( rawType );
60-
break;
61-
default:
62-
throw new NotImplementedException( "Invalid mode." );
63-
}
64-
63+
this.Type = reader.ReadInt32();
6564
this.Scale = (float)reader.ReadSingle();
6665
this.Color = new Color(
6766
(byte)reader.ReadByte(),
@@ -85,7 +84,7 @@ public override void Read( BinaryReader reader ) {
8584

8685
public override void Write( BinaryWriter writer ) {
8786
writer.Write( (ushort)this.Mode );
88-
writer.Write( (string)this.TypeDef.ToString() );
87+
writer.Write( (int)this.Type );
8988
writer.Write( (float)this.Scale );
9089
writer.Write( (byte)this.Color.R );
9190
writer.Write( (byte)this.Color.G );

0 commit comments

Comments
 (0)