Skip to content

Commit

Permalink
> Fixed: gob matches very memory uses
Browse files Browse the repository at this point in the history
> Fixed: map wait issues
> Changed: ui scale step from 25 to 5
+Update
  • Loading branch information
Cediner committed Apr 12, 2024
1 parent bf8b828 commit 01cdf5b
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<project name="hafen" default="deftgt">
<property name="name" value="ArdClient"/>
<property name="version" value="2024.04.06"/>
<property name="version" value="2024.04.12"/>

<target name="build-env">
<mkdir dir="build"/>
Expand Down
68 changes: 52 additions & 16 deletions src/haven/Gob.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import modification.resources;

import java.awt.Color;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -68,12 +69,14 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Supplier;

public class Gob implements Rendered, Sprite.Owner, Skeleton.ModOwner, Skeleton.HasPose, EquipTarget {
public int cropstgmaxval = 0;
Expand Down Expand Up @@ -248,12 +251,12 @@ public void remove(Gob gob) {
@Override
public boolean setup(RenderList rl) {
if (spr != null) {
if (name().matches("gfx/terobjs/trees/yulestar-.*")) {
if (name().matches(".*fir")) {
if (matches(this, this::name,"gfx/terobjs/trees/yulestar-.*")) {
if (matches(this, this::name,".*fir")) {
rl.prepc(Location.xlate(Coord3f.of((float) -0.655989, (float) 0.183716, (float) 48.3776)));
} else if (name().matches(".*spruce")) {
} else if (matches(this, this::name,".*spruce")) {
rl.prepc(Location.xlate(Coord3f.of(0f, (float) -3.055197, (float) 62.988228)));
} else if (name().matches(".*silverfir")) {
} else if (matches(this, this::name,".*silverfir")) {
rl.prepc(Location.xlate(Coord3f.of((float) -0.649652, (float) -0.030299, (float) 92.28412)));
}
rl.prepc(Location.rot(Coord3f.of(0f, 1f, 0f), (float) 1.570796));
Expand Down Expand Up @@ -809,17 +812,17 @@ private void discovered(final String name) {
if (findol(-4921) == null)
addol(new Overlay(-4921, new SnowFall(this)));
}
if (type == Type.UNKNOWN && name.matches("gfx/terobjs/trees/[a-zA-Z\\-]+stump$"))
if (type == Type.UNKNOWN && matches(this, () -> name,"gfx/terobjs/trees/[a-zA-Z\\-]+stump$"))
type = Type.STUMP;
if (type == Type.UNKNOWN && name.matches("gfx/terobjs/trees/[a-zA-Z\\-]+log$"))
if (type == Type.UNKNOWN && matches(this, () -> name,"gfx/terobjs/trees/[a-zA-Z\\-]+log$"))
type = Type.LOG;
if (type == Type.UNKNOWN && name.matches("gfx/terobjs/trees/[a-zA-Z\\-]+$"))
if (type == Type.UNKNOWN && matches(this, () -> name,"gfx/terobjs/trees/[a-zA-Z\\-]+$"))
type = Type.TREE;
if (type == Type.UNKNOWN && name.matches("gfx/terobjs/plants/(?!trellis).*"))
if (type == Type.UNKNOWN && matches(this, () -> name,"gfx/terobjs/plants/(?!trellis).*"))
type = Type.PLANT;

String customIcon = null;
if ((type == Type.TREE || type == Type.BUSH || type == Type.STUMP || type == Type.LOG) && (!name.matches(".*trees/old(stump|trunk)"))) {
if ((type == Type.TREE || type == Type.BUSH || type == Type.STUMP || type == Type.LOG) && (!matches(this, () -> name,".*trees/old(stump|trunk)"))) {
String fistname1 = name.substring(0, name.lastIndexOf('/'));
String fistname = fistname1.substring(0, fistname1.lastIndexOf('/'));
String lastname = name.replace(fistname, "");
Expand Down Expand Up @@ -1567,6 +1570,39 @@ public void draw(GOut g) {}
private gobText barreltext;
private gobText treetext;

private static final Map<Object, WeakReference<SavedMatches>> matchesMap = Collections.synchronizedMap(new HashMap<>());
private static class SavedMatches {
String name;
Map<String, Boolean> matches = Collections.synchronizedMap(new HashMap<>());
}

static boolean matches(Object obj, Supplier<String> getter, String regex) {
synchronized (matchesMap) {
WeakReference<SavedMatches> wrm = matchesMap.get(obj);
SavedMatches saved = wrm == null ? null : wrm.get();
String name = getter.get();
if (saved == null) {
saved = new SavedMatches();
saved.name = name;
boolean result = name.matches(regex);
saved.matches.put(regex, result);
matchesMap.put(obj, new WeakReference<>(saved));
return (result);
} else {
if (!Objects.equals(name, saved.name)) {
saved = new SavedMatches();
saved.name = name;
boolean result = name.matches(regex);
saved.matches.put(regex, result);
matchesMap.put(obj, new WeakReference<>(saved));
return (result);
} else {
return (saved.matches.computeIfAbsent(regex, name::matches));
}
}
}
}

@Override
public boolean setup(RenderList rl) {
loc.tick();
Expand Down Expand Up @@ -1609,15 +1645,15 @@ public boolean setup(RenderList rl) {
boolean barrelRet = true;
for (Overlay ol : ols) {
String olname = ol.name();
if (olname.matches("gfx/terobjs/trees/yulestar-.*")) {
if (ol.spr == null || ol.spr.res == null || ol.spr.res.name.matches("gfx/terobjs/trees/yulestar-.*"))
if (matches(ol, () -> olname,"gfx/terobjs/trees/yulestar-.*")) {
if (ol.spr == null || ol.spr.res == null || matches(ol.spr, () -> ol.spr.res.name,"gfx/terobjs/trees/yulestar-.*"))
ol.spr = Sprite.create(this, Resource.remote().loadwait("gfx/terobjs/items/yulestar"), ol.sdt);
}
if (name().matches("gfx/terobjs/barrel")) {
if (Config.showbarreltext && barrelText == null && olname.matches("gfx/terobjs/barrel-.*")) {
if (matches(this, this::name,"gfx/terobjs/barrel")) {
if (Config.showbarreltext && barrelText == null && matches(ol, () -> olname,"gfx/terobjs/barrel-.*")) {
barrelText = olname.substring(olname.lastIndexOf("-") + 1);
}
if (Config.showbarrelstatus && barrelRet && olname.matches("gfx/terobjs/barrel-.*")) {
if (Config.showbarrelstatus && barrelRet && matches(ol, () -> olname,"gfx/terobjs/barrel-.*")) {
barrelRet = false;
}
}
Expand All @@ -1627,7 +1663,7 @@ public boolean setup(RenderList rl) {
if (ol.spr instanceof Overlay.SetupMod)
((Overlay.SetupMod) ol.spr).setupmain(rl);
}
if (Config.showbarreltext && name().matches("gfx/terobjs/barrel") && !ols.isEmpty()) {
if (Config.showbarreltext && matches(this, this::name,"gfx/terobjs/barrel") && !ols.isEmpty()) {
if (barrelText != null) {
if (barreltext == null) barreltext = new gobText(this, barrelText, Color.GREEN, 50);
else barreltext.update(barrelText);
Expand All @@ -1638,7 +1674,7 @@ public boolean setup(RenderList rl) {
} else {
if (barreltext != null) barreltext = null;
}
if (Config.showbarrelstatus && name().matches("gfx/terobjs/barrel")) {
if (Config.showbarrelstatus && matches(this, this::name,"gfx/terobjs/barrel")) {
if (barrelRet) rl.prepc(barrelemptycolormaterial.get());
}
for (Map.Entry<Long, String> entry : configuration.treesMap.entrySet()) {
Expand Down
3 changes: 2 additions & 1 deletion src/haven/HavenPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.jogamp.opengl.util.awt.Screenshot;
import haven.sloth.util.ObservableCollection;
import modification.configuration;
import modification.dev;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
Expand Down Expand Up @@ -858,7 +859,7 @@ public void run() {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
e.printStackTrace();
dev.simpleLog(e);
closeCurrentSession();
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/haven/LoginScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Scanner;
Expand Down Expand Up @@ -319,6 +322,8 @@ private void setHoverItem(Coord c) {
}
}

private final Map<String, Tex> cached = Collections.synchronizedMap(new HashMap<>());

@Override
protected void drawitem(GOut g, LoginData item, int i) {
if (hover == i) {
Expand All @@ -329,7 +334,7 @@ protected void drawitem(GOut g, LoginData item, int i) {
g.chcolor(68, 68, 68, 128);
g.rect(Coord.z, g.sz);
g.chcolor();
Tex tex = Text.render(item.name, Color.WHITE, textfs).tex();
Tex tex = cached.computeIfAbsent(item.name, (text) -> Text.render(text, Color.WHITE, textfs).tex());
g.image(tex, new Coord(UI.scale(5), (ITEM_HEIGHT - tex.sz().y) / 2));
g.chcolor(xhover == i ? Color.YELLOW : Color.RED);
g.aimage(xicon, new Coord(sz.x - UI.scale(5), (ITEM_HEIGHT - xicon.sz().y) / 2), 1, 0);
Expand Down
25 changes: 21 additions & 4 deletions src/haven/MapFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ public void updategrids(MCache map, Collection<MCache.Grid> grids) {

public void removeSegment(MapFileWidget.Location loc) {
lock.writeLock().lock();
segments.remove(loc.seg.id);
Path mf = Utils.pj(HashDirCache.findbase(), mangle(String.format("seg-%x", loc.seg.id)));
try {
segments.remove(loc.seg.id);
Path mf = Utils.pj(HashDirCache.findbase(), mangle(String.format("seg-%x", loc.seg.id)));
Files.deleteIfExists(mf);
} catch (Exception ignored) {
} catch (Throwable e) {
dev.simpleLog(e);
} finally {
lock.writeLock().unlock();
}
lock.writeLock().unlock();
}

@Deprecated
Expand Down Expand Up @@ -292,6 +294,9 @@ private static Runnable locked(Runnable r, Lock lock) {
lock.lock();
try {
r.run();
} catch (Throwable e) {
dev.simpleLog(e);
throw (e);
} finally {
lock.unlock();
}
Expand All @@ -303,6 +308,9 @@ private static <A, R> Function<A, R> locked(Function<A, R> f, Lock lock) {
lock.lock();
try {
return (f.apply(v));
} catch (Throwable e) {
dev.simpleLog(e);
throw (e);
} finally {
lock.unlock();
}
Expand Down Expand Up @@ -488,6 +496,9 @@ public void remove(Marker mark) {
defersave();
markerseq++;
}
} catch (Throwable e) {
dev.simpleLog(e);
throw (e);
} finally {
lock.writeLock().unlock();
}
Expand Down Expand Up @@ -2160,6 +2171,9 @@ public void update(MCache map, Collection<MCache.Grid> grids) {
merge(dst, src, soff);
}
}
} catch (Throwable e) {
dev.simpleLog(e);
throw (e);
} finally {
lock.writeLock().unlock();
}
Expand Down Expand Up @@ -2555,6 +2569,9 @@ void importgrid(boolean errors, Message data) {
gridinfo.put(rgrid.id, new GridInfo(rgrid.id, rseg.id, nc));
}
}
} catch (Throwable e) {
dev.simpleLog(e);
throw (e);
} finally {
lock.writeLock().unlock();
}
Expand Down
6 changes: 4 additions & 2 deletions src/haven/MapFileWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import haven.MapFile.Segment;
import integrations.mapv4.MappingClient;
import modification.configuration;
import modification.dev;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
Expand Down Expand Up @@ -735,8 +736,9 @@ public boolean mousedown(Coord c, int button) {

file.updategrids(ui.sess.glob.map, ui.sess.glob.map.grids.values());
refreshDisplayGrid();
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable e) {
dev.simpleLog(e);
throw (e);
} finally {
file.lock.writeLock().unlock();
}
Expand Down
9 changes: 5 additions & 4 deletions src/haven/MapWnd.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,15 @@ public void click() {
add(new IButton("gfx/hud/worldmap/plus", "", "", "") {
@Override
public void click() {
if (view.zoom > MapFileWidget.zoommin) {
MapFileWidget.Location loc = view.curloc;
if (loc != null && view.zoom > MapFileWidget.zoommin) {
zoomtex = null;
Coord tc = view.curloc.tc.mul(view.scalef());
Coord tc = loc.tc.mul(view.scalef());
view.zoom--;
Utils.setprefi("zoomlmap", view.zoom);
tc = tc.div(view.scalef());
view.curloc.tc.x = tc.x;
view.curloc.tc.y = tc.y;
loc.tc.x = tc.x;
loc.tc.y = tc.y;
}
}
}, new Coord(btnsz + UI.scale(20), 0));
Expand Down
4 changes: 2 additions & 2 deletions src/haven/OptWnd.java
Original file line number Diff line number Diff line change
Expand Up @@ -6567,8 +6567,8 @@ public void changed() {
}
{
Label dpy = new Label("");
final double smin = 1, smax = Math.floor(UI.maxscale() / 0.25) * 0.25;
final int steps = (int) Math.round((smax - smin) / 0.25);
final double smin = 1, smax = Math.floor(UI.maxscale() / 0.05) * 0.05;
final int steps = (int) Math.round((smax - smin) / 0.05);
appender.addRow(new Label("UI scale (req restart)"), new HSlider(UI.scale(160), 0, steps, (int) Math.round(steps * (Utils.getprefd("uiscale", 1.0) - smin) / (smax - smin))) {
@Override
protected void added() {
Expand Down
8 changes: 4 additions & 4 deletions src/haven/ResDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public void setup(RenderList rl) {
if (!inited) return;
try {
String name = getres().name;
if (name.matches("gfx/terobjs/trees/yulestar-.*")) {
if (name.matches(".*fir")) {
if (Gob.matches(this, () -> name, "gfx/terobjs/trees/yulestar-.*")) {
if (Gob.matches(this, () -> name, ".*fir")) {
rl.prepc(Location.xlate(Coord3f.of((float) -0.655989, (float) 0.183716, (float) 48.3776)));
} else if (name.matches(".*spruce")) {
} else if (Gob.matches(this, () -> name, ".*spruce")) {
rl.prepc(Location.xlate(Coord3f.of(0f, (float) -3.055197, (float) 62.988228)));
} else if (name.matches(".*silverfir")) {
} else if (Gob.matches(this, () -> name, ".*silverfir")) {
rl.prepc(Location.xlate(Coord3f.of((float) -0.649652, (float) -0.030299, (float) 92.28412)));
}
rl.prepc(Location.rot(Coord3f.of(0f, 1f, 0f), (float) 1.570796));
Expand Down
4 changes: 3 additions & 1 deletion src/haven/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void waitfor(Runnable callback, Consumer<Waitable.Waiting> reg) {
public void waitfor() throws InterruptedException {
synchronized (res) {
while (res.resnm == null)
res.wait();
res.wait(1000);
}
}

Expand Down Expand Up @@ -204,6 +204,7 @@ public void set(String nm, int ver) {
this.resver = ver;
get().reset();
wq.wnotify();
notifyAll();
}
}

Expand All @@ -213,6 +214,7 @@ public void set(Resource res) {
this.resver = res.ver;
ind = new WeakReference<>(new SRef(res));
wq.wnotify();
notifyAll();
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions src/haven/res/gfx/tiles/paving/stranglevine/Factory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package haven.res.gfx.tiles.paving.stranglevine;

import haven.FastMesh;
import haven.Message;
import haven.Resource;
import haven.Sprite;
import haven.resutil.CSprite;

import java.util.ArrayList;
import java.util.Random;

@haven.FromResource(name = "gfx/tiles/paving/stranglevine", version = 13, override = true)
public class Factory implements Sprite.Factory {
public static final int[] dnum = {3, 6, 12};
public static final float r = 3;

public Sprite create(Sprite.Owner owner, Resource res, Message sdt) {
ArrayList<FastMesh.MeshRes> var = new ArrayList<FastMesh.MeshRes>(res.layers(FastMesh.MeshRes.class));
Random rnd = owner.mkrandoom();
CSprite spr = new CSprite(owner, res);
int st = sdt.eom() ? 2 : sdt.uint8();
int num = rnd.nextInt(dnum[st]) + dnum[st];
for (int i = 0; i < num; i++) {
FastMesh.MeshRes v = var.get(rnd.nextInt(var.size()));
spr.addpart((float) rnd.nextGaussian() * r, (float) rnd.nextGaussian() * r, v.mat.get(), v.m);
}
return (spr);
}
}
Binary file modified update/build/hafen.jar
Binary file not shown.

0 comments on commit 01cdf5b

Please sign in to comment.