Skip to content

Commit d5a9229

Browse files
committed
checking performance, pushing what I have rn
1 parent a54cef2 commit d5a9229

File tree

8 files changed

+249
-75
lines changed

8 files changed

+249
-75
lines changed

src/main/java/llc/redstone/hysentials/Hysentials.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ public void onFMLPreInitialization(FMLPreInitializationEvent event) {
151151

152152
@Mod.EventHandler
153153
public void init(FMLInitializationEvent event) {
154-
registerImages();
155154
config = new HysentialsConfig();
156155
File file = new File(modDir, "./config/hysentials");
157156
if (!file.exists() && !file.mkdirs()) {
@@ -262,6 +261,7 @@ public void postInit(FMLPostInitializationEvent event) {
262261
FancyFormattingKt.getChars().put((char) i, Minecraft.getMinecraft().fontRendererObj.getCharWidth((char) i));
263262
}
264263
}
264+
registerImages();
265265
updateAndAdd();
266266
}
267267
@Mod.EventHandler

src/main/java/llc/redstone/hysentials/config/hysentialmods/FormattingConfig.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,21 @@ public FormattingConfig() {
263263
addDependency("fancyRankInTab", "futuristicRanks");
264264
addDependency("fancyRankInChat", "futuristicRanks");
265265

266-
addListener("futuristicRanks", () -> {
267-
if (FormattingConfig.fancyRendering()) {
268-
Minecraft.getMinecraft().fontRendererObj = Hysentials.INSTANCE.imageIconRenderer;
269-
} else {
270-
Minecraft.getMinecraft().fontRendererObj = Hysentials.minecraftFont;
271-
}
272-
});
273-
274-
addListener("hexColors", () -> {
275-
if (FormattingConfig.fancyRendering()) {
276-
Minecraft.getMinecraft().fontRendererObj = Hysentials.INSTANCE.imageIconRenderer;
277-
} else {
278-
Minecraft.getMinecraft().fontRendererObj = Hysentials.minecraftFont;
279-
}
280-
});
266+
// addListener("futuristicRanks", () -> {
267+
// if (FormattingConfig.fancyRendering()) {
268+
// Minecraft.getMinecraft().fontRendererObj = Hysentials.INSTANCE.imageIconRenderer;
269+
// } else {
270+
// Minecraft.getMinecraft().fontRendererObj = Hysentials.minecraftFont;
271+
// }
272+
// });
273+
//
274+
// addListener("hexColors", () -> {
275+
// if (FormattingConfig.fancyRendering()) {
276+
// Minecraft.getMinecraft().fontRendererObj = Hysentials.INSTANCE.imageIconRenderer;
277+
// } else {
278+
// Minecraft.getMinecraft().fontRendererObj = Hysentials.minecraftFont;
279+
// }
280+
// });
281281
}
282282

283283
public void initialize() {

src/main/java/llc/redstone/hysentials/handlers/imageicons/ImageIcon.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ImageIcon {
3939
public int width;
4040
public int height;
4141
public boolean emoji;
42-
42+
public Character firstChar = null;
4343
public String replacement = null;
4444

4545
public ImageIcon(String name, ResourceLocation resourceLocation, boolean emoji) {
@@ -141,50 +141,56 @@ public int getHeight() {
141141
return height;
142142
}
143143

144-
public static Pattern stringPattern = Pattern.compile(":([a-z_\\-0-9?]+):", 2);
145-
146144
public void setReplacement() {
147145
int width = this.getWidth();
148146
int height = this.getHeight();
149147
float scaledHeight = 9.0f / height;
150-
float updatedHeight = height * scaledHeight;
151148
float updatedWidth = width * scaledHeight;
152149

153150
int currentWidth = 0;
154151
boolean isFirst = true;
155-
AtomicBoolean breaks = new AtomicBoolean(false);
156152
StringBuilder string = new StringBuilder();
157-
while (currentWidth < updatedWidth) {
153+
while (currentWidth <= updatedWidth) {
158154
Integer charWidth = null;
159155
for (Map.Entry<Character, Integer> entry : getChars().entrySet()) {
160-
if (entry.getValue() + currentWidth <= updatedWidth) {
161-
charWidth = entry.getValue() + 1;
156+
if (entry.getValue() + 1 + currentWidth <= updatedWidth) {
157+
charWidth = entry.getValue();
162158
break;
163159
}
164160
}
165161
if (charWidth == null) {
166162
break;
167163
}
168-
currentWidth += charWidth;
164+
currentWidth += charWidth + 1;
169165
List<Character> charKeys = new ArrayList<>();
170166
for (Map.Entry<Character, Integer> entry : getChars().entrySet()) {
171167
if (entry.getValue().equals(charWidth)) {
172-
if (isFirst) {
173-
imageIcons.values().stream().filter(icon -> icon.replacement != null && !icon.replacement.isEmpty() && icon.replacement.charAt(0) == entry.getKey()).findFirst().ifPresent(icon -> {
174-
breaks.set(true);
175-
});
176-
if (breaks.get()) {
177-
continue;
178-
}
179-
isFirst = false;
180-
}
181168
charKeys.add(entry.getKey());
182169
}
183170
}
184-
char charKey = charKeys.get(new Random().nextInt(charKeys.size()));
171+
char charKey;
172+
if (isFirst) {
173+
charKey = charKeys.get(new Random().nextInt(charKeys.size()));
174+
for (ImageIcon icon : ImageIcon.imageIcons.values()) {
175+
if (icon.firstChar == charKey) {
176+
icon.setReplacement();
177+
break;
178+
}
179+
}
180+
firstChar = charKey;
181+
isFirst = false;
182+
} else if (!charKeys.isEmpty()) {
183+
charKey = charKeys.get(new Random().nextInt(charKeys.size()));
184+
} else {
185+
currentWidth -= charWidth + 1;
186+
continue;
187+
}
185188
string.append(charKey);
186189
}
187-
System.out.println("Using " + string + " for " + name);
190+
if (string.length() == 0) {
191+
setReplacement();
192+
return;
193+
}
188194
replacement = string.toString();
189195
}
190196

@@ -195,6 +201,7 @@ public float renderImage(float x, float y, boolean shadow, int oldColor, float a
195201
float updatedHeight = height * scaledHeight;
196202
float updatedWidth = width * scaledHeight;
197203

204+
GlStateManager.pushMatrix();
198205
dynamicTexture.updateDynamicTexture();
199206

200207
int textColor = Integer.parseInt("FFFFFF", 16);
@@ -203,7 +210,8 @@ public float renderImage(float x, float y, boolean shadow, int oldColor, float a
203210
}
204211
GlStateManager.color((float) (textColor >> 16) / 255.0F, (float) (textColor >> 8 & 255) / 255.0F, (float) (textColor & 255) / 255.0F, alpha);
205212
drawModalRectWithCustomSizedTexture(x, y, 0f, 0f, updatedWidth, updatedHeight, updatedWidth, updatedHeight);
206-
GlStateManager.color((float) (oldColor >> 16) / 255.0F, (float) (oldColor >> 8 & 255) / 255.0F, (float) (oldColor & 255) / 255.0F, alpha);
213+
// GlStateManager.color((float) (oldColor >> 16) / 255.0F, (float) (oldColor >> 8 & 255) / 255.0F, (float) (oldColor & 255) / 255.0F, alpha);
214+
GlStateManager.popMatrix();
207215
return (int) (width * scaledHeight);
208216
}
209217

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package llc.redstone.hysentials.mixin.fancyformatting;
2+
3+
import llc.redstone.hysentials.config.hysentialmods.FormattingConfig;
4+
import llc.redstone.hysentials.renderer.text.FancyFormattingKt;
5+
import llc.redstone.hysentials.util.ImageIconRenderer;
6+
import net.minecraft.client.gui.FontRenderer;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.Shadow;
9+
import org.spongepowered.asm.mixin.Unique;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
13+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
14+
15+
@Mixin(value = FontRenderer.class, priority = 1004)
16+
public abstract class HexColorMixin {
17+
@Shadow
18+
private int textColor;
19+
@Shadow
20+
private float alpha;
21+
@Shadow
22+
protected float posX;
23+
@Shadow
24+
protected float posY;
25+
26+
@Shadow
27+
protected abstract float renderChar(char ch, boolean italic);
28+
29+
@Shadow
30+
protected abstract void doDraw(float f);
31+
32+
33+
@Shadow
34+
protected abstract void setColor(float r, float g, float b, float a);
35+
36+
@Unique
37+
Boolean hysentials2$shadow = null;
38+
@Unique
39+
Boolean hysentials2$working = false;
40+
@Unique
41+
Boolean hysentials2$onemore = false;
42+
@Unique
43+
String hysentials2$current = null;
44+
45+
@ModifyVariable(method = "renderStringAtPos", at = @At(value = "INVOKE", target = "Ljava/lang/String;charAt(I)C", ordinal = 0, shift = At.Shift.AFTER))
46+
private int doSomeHexify(int index) {
47+
if (!FormattingConfig.hexRendering()) return index;
48+
if (FancyFormattingKt.getCurrentText() == null) return index;
49+
String text = FancyFormattingKt.getCurrentText();
50+
char c0 = text.charAt(index);
51+
52+
if (c0 == '<' && text.substring(index).length() > 8 && text.charAt(index + 1) == '#' && text.charAt(index + 8) == '>') {
53+
hysentials2$current = "<#";
54+
hysentials2$working = true;
55+
return index;
56+
}
57+
if (!hysentials2$working || hysentials2$current == null) return index;
58+
if (hysentials$isHex(c0) || c0 == '>') {
59+
hysentials2$current += c0;
60+
}
61+
62+
if (hysentials2$current.length() == 9 && hysentials2$current.charAt(8) == '>') {
63+
String hex = hysentials2$current.substring(2, 8);
64+
if (!ImageIconRenderer.checkIfHexadecimal(hex)) return index;
65+
int color = Integer.parseInt(hex, 16);
66+
if (hysentials2$shadow) {
67+
color = (color & 16579836) >> 2 | color & -16777216;
68+
}
69+
textColor = color;
70+
this.setColor((float)(color >> 16) / 255.0F, (float)(color >> 8 & 255) / 255.0F, (float)(color & 255) / 255.0F, this.alpha);
71+
72+
hysentials2$onemore = true;
73+
resetHysentialsState();
74+
return index;
75+
}
76+
return index;
77+
}
78+
79+
@Unique
80+
private void resetHysentialsState() {
81+
hysentials2$working = false;
82+
hysentials2$current = null;
83+
}
84+
85+
@Unique
86+
private boolean hysentials$isHex(char ch) {
87+
return ('0' <= ch && ch <= '9') ||
88+
('a' <= ch && ch <= 'f') ||
89+
('A' <= ch && ch <= 'F');
90+
}
91+
92+
@ModifyVariable(method = "renderStringAtPos", at = @At("HEAD"), argsOnly = true, index = 2)
93+
private boolean renderStringAtPosReplace2(boolean value) {
94+
hysentials2$shadow = value;
95+
return value;
96+
}
97+
98+
@Inject(method = "renderChar", at = @At("HEAD"), cancellable = true)
99+
private void renderCharReplace2(char ch, boolean italic, CallbackInfoReturnable<Float> cir) {
100+
if (hysentials2$working || hysentials2$onemore) {
101+
cir.setReturnValue(0f);
102+
if (hysentials2$onemore) hysentials2$onemore = false;
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)