public void mapdata(Message msg) { long now = System.currentTimeMillis(); int pktid = msg.int32(); int off = msg.uint16(); int len = msg.uint16(); Defrag fragbuf; synchronized (fragbufs) { if ((fragbuf = fragbufs.get(pktid)) == null) { fragbuf = new Defrag(len); fragbufs.put(pktid, fragbuf); } fragbuf.add(msg.blob, 8, msg.blob.length - 8, off); fragbuf.last = now; if (fragbuf.done()) { mapdata2(fragbuf.msg()); fragbufs.remove(pktid); } /* Clean up old buffers */ for (Iterator<Map.Entry<Integer, Defrag>> i = fragbufs.entrySet().iterator(); i.hasNext(); ) { Map.Entry<Integer, Defrag> e = i.next(); Defrag old = e.getValue(); if (now - old.last > 10000) i.remove(); } } }
public SpriteLink(Resource res, Message buf) { res.super(); int ver = buf.uint8(); if (ver != 1) throw (new Resource.LoadException("Unknown spritelink version: " + ver, getres())); Map<Integer, Factory> refs = new IntMap<Factory>(16); while (true) { int id = buf.int16(); if (id < 0) break; char t = (char) buf.uint8(); Factory f; switch (t) { case 't': f = new ByTile(res, buf, refs); break; case 'r': f = new ByRes(res, buf, refs); break; default: throw (new Resource.LoadException("Unknown spritelink type: `" + t + "'", getres())); } refs.put(id, f); } this.f = refs.get(buf.int16()); }
private static Color col(Message buf) { return (new Color( (int) (buf.cpfloat() * 255.0), (int) (buf.cpfloat() * 255.0), (int) (buf.cpfloat() * 255.0), (int) (buf.cpfloat() * 255.0))); }
public void tilemap(Message msg) { while (!msg.eom()) { int id = msg.uint8(); String resnm = msg.string(); int resver = msg.uint16(); nsets[id] = new Resource.Spec(resnm, resver); } }
private ByTile(Resource res, Message buf, Map<Integer, Factory> refs) { tag = new String[buf.uint8()]; sub = new Factory[tag.length]; for (int i = 0; i < tag.length; i++) { tag[i] = buf.string(); sub[i] = refs.get(buf.int16()); } def = refs.get(buf.int16()); }
public static int decnum(Message sdt) { if (sdt == null) return (0); int ret = 0, off = 0; while (!sdt.eom()) { ret |= sdt.uint8() << off; off += 8; } return (ret); }
public void append(Message msg) { synchronized (msgs) { msgs.add(msg); int y = 0; for (Message m : msgs) y += m.sz().y; boolean b = sb.val >= sb.max; sb.max = y - ih(); if (b) sb.val = sb.max; } }
public void run() { try { InputStream in; OutputStream out; try { in = sk.getInputStream(); out = sk.getOutputStream(); } catch (IOException e) { throw (new RuntimeException(e)); } while (true) { try { int len = Utils.int32d(read(in, 4), 0); if (!auth && (len > 256)) return; Message msg = new MessageBuf(read(in, len)); String cmd = msg.string(); Object[] args = msg.list(); Object[] reply; if (auth) { Command cc = commands.get(cmd); if (cc != null) reply = cc.run(this, args); else reply = new Object[] {"nocmd"}; } else { if (cmd.equals("nonce")) { reply = new Object[] {nonce}; } else if (cmd.equals("auth")) { if (Arrays.equals((byte[]) args[0], ckey)) { reply = new Object[] {"ok"}; auth = true; } else { reply = new Object[] {"no"}; } } else { return; } } MessageBuf rb = new MessageBuf(); rb.addlist(reply); byte[] rbuf = new byte[4 + rb.size()]; Utils.uint32e(rb.size(), rbuf, 0); rb.fin(rbuf, 4); out.write(rbuf); } catch (IOException e) { return; } } } catch (InterruptedException e) { } finally { try { sk.close(); } catch (IOException e) { throw (new RuntimeException(e)); } } }
protected void selected(CharPos start, CharPos end) { StringBuilder buf = new StringBuilder(); synchronized (msgs) { boolean sel = false; for (Message msg : msgs) { if (!(msg.text() instanceof RichText)) continue; RichText rt = (RichText) msg.text(); RichText.Part part = null; if (sel) { part = rt.parts; } else if (msg == start.msg) { sel = true; for (part = rt.parts; part != null; part = part.next) { if (part == start.part) break; } } if (sel) { for (; part != null; part = part.next) { if (!(part instanceof RichText.TextPart)) continue; RichText.TextPart tp = (RichText.TextPart) part; CharacterIterator iter = tp.ti(); int sch; if (tp == start.part) sch = tp.start + start.ch.getInsertionIndex(); else sch = tp.start; int ech; if (tp == end.part) ech = tp.start + end.ch.getInsertionIndex(); else ech = tp.end; for (int i = sch; i < ech; i++) buf.append(iter.setIndex(i)); if (part == end.part) { sel = false; break; } buf.append(' '); } if (sel) buf.append('\n'); } if (msg == end.msg) break; } } Clipboard cl; if ((cl = java.awt.Toolkit.getDefaultToolkit().getSystemSelection()) == null) cl = java.awt.Toolkit.getDefaultToolkit().getSystemClipboard(); try { final CharPos ownsel = selstart; cl.setContents( new StringSelection(buf.toString()), new ClipboardOwner() { public void lostOwnership(Clipboard cl, Transferable tr) { if (selstart == ownsel) selstart = selend = null; } }); } catch (IllegalStateException e) { } }
public void invalblob(Message msg) { int type = msg.uint8(); if (type == 0) { invalidate(msg.coord()); } else if (type == 1) { Coord ul = msg.coord(); Coord lr = msg.coord(); trim(ul, lr); } else if (type == 2) { trimall(); } }
public void resize(Coord sz) { super.resize(sz); if (sb != null) { sb.resize(ih()); sb.move(new Coord(sz.x, 0)); int y = 0; for (Message m : msgs) y += m.sz().y; boolean b = sb.val >= sb.max; sb.max = y - ih(); if (b) sb.val = sb.max; } if (cbtn != null) cbtn.c = new Coord(sz.x - cbtn.sz.x - sb.sz.x - 3, 0); }
private void drawsel(GOut g, Message msg, int y) { RichText rt = (RichText) msg.text(); boolean sel = msg != selstart.msg; for (RichText.Part part = rt.parts; part != null; part = part.next) { if (!(part instanceof RichText.TextPart)) continue; RichText.TextPart tp = (RichText.TextPart) part; if (tp.start == tp.end) continue; TextHitInfo a, b; if (sel) { a = TextHitInfo.leading(0); } else if (tp == selstart.part) { a = selstart.ch; sel = true; } else { continue; } if (tp == selend.part) { sel = false; b = selend.ch; } else { b = TextHitInfo.trailing(tp.end - tp.start - 1); } Coord ul = new Coord(tp.x + (int) tp.advance(0, a.getInsertionIndex()), tp.y + y); Coord sz = new Coord((int) tp.advance(a.getInsertionIndex(), b.getInsertionIndex()), tp.height()); g.chcolor(0, 0, 255, 255); g.frect(ul, sz); g.chcolor(); if (!sel) break; } }
public Message messageat(Coord c, Coord hc) { int y = -sb.val; synchronized (msgs) { for (Message msg : msgs) { Coord sz = msg.sz(); if ((c.y >= y) && (c.y < y + sz.y)) { if (hc != null) { hc.x = c.x; hc.y = c.y - y; } return (msg); } y += sz.y; } } return (null); }
public String tryauth(AuthClient cl) throws IOException { if (WebBrowser.self == null) { throw (new AuthException("Could not find any web browser to launch")); } Message rpl = cl.cmd("web", method()); String stat = rpl.string(); URL url; if (stat.equals("ok")) { url = new URL(rpl.string()); } else if (stat.equals("no")) { throw (new AuthException(rpl.string())); } else { throw (new RuntimeException("Unexpected reply `" + stat + "' from auth server")); } try { WebBrowser.self.show(url); } catch (WebBrowser.BrowserException e) { throw (new AuthException("Could not launch web browser")); } rpl = cl.cmd("wait"); stat = rpl.string(); if (stat.equals("ok")) { return (rpl.string()); } else if (stat.equals("no")) { throw (new AuthException(rpl.string())); } else { throw (new RuntimeException("Unexpected reply `" + stat + "' from auth server")); } }
public CharPos charat(Coord c) { if (c.y < -sb.val) { if (msgs.size() < 1) return (null); Message msg = msgs.get(0); if (!(msg.text() instanceof RichText)) return (null); RichText.TextPart fp = null; for (RichText.Part part = ((RichText) msg.text()).parts; part != null; part = part.next) { if (part instanceof RichText.TextPart) { fp = (RichText.TextPart) part; break; } } if (fp == null) return (null); return (new CharPos(msg, fp, TextHitInfo.leading(0))); } Coord hc = new Coord(); Message msg = messageat(c, hc); if ((msg == null) || !(msg.text() instanceof RichText)) return (null); RichText rt = (RichText) msg.text(); RichText.Part p = rt.partat(hc); if (p == null) { RichText.TextPart lp = null; for (RichText.Part part = ((RichText) msg.text()).parts; part != null; part = part.next) { if (part instanceof RichText.TextPart) lp = (RichText.TextPart) part; } if (lp == null) return (null); return (new CharPos(msg, lp, TextHitInfo.trailing(lp.end - lp.start - 1))); } if (!(p instanceof RichText.TextPart)) return (null); RichText.TextPart tp = (RichText.TextPart) p; return (new CharPos(msg, tp, tp.charat(hc))); }
public void sendreqs() { long now = System.currentTimeMillis(); synchronized (req) { for (Iterator<Map.Entry<Coord, Request>> i = req.entrySet().iterator(); i.hasNext(); ) { Map.Entry<Coord, Request> e = i.next(); Coord c = e.getKey(); Request r = e.getValue(); if (now - r.lastreq > 1000) { r.lastreq = now; if (++r.reqs >= 5) { i.remove(); } else { Message msg = new Message(Session.MSG_MAPREQ); msg.addcoord(c); sess.sendmsg(msg); } } } } }
/* */ public Sprite create(Sprite.Owner owner, Resource res, Message sdt) { /* 61 */ int m = sdt.uint8(); /* 62 */ GrowingPlant spr = new GrowingPlant(owner, res); /* 63 */ spr.addnegative(); /* 64 */ Random rnd = owner.mkrandoom(); /* 65 */ for (int i = 0; i < this.num; i++) { /* 66 */ Coord c = new Coord(rnd.nextInt(this.neg.bs.x), rnd.nextInt(this.neg.bs.y)).add(this.neg.bc); /* 67 */ Tex s = this.strands[m][rnd.nextInt(this.strands[m].length)]; /* 68 */ spr.add(s, 0, MapView.m2s(c), new Coord(s.sz().x / 2, s.sz().y).inv()); /* */ } /* 70 */ return spr; /* */ }
public Res cons(Resource res, Message buf) { int id = buf.uint16(); Res ret = new Res(res, id); while (!buf.eom()) { String nm = buf.string(); Object[] args = buf.list(); if (nm.equals("linear")) { /* XXX: These should very much be removed and * specified directly in the texture layer * instead. */ ret.linear = true; } else if (nm.equals("mipmap")) { ret.mipmap = true; } else { ResCons2 cons = rnames.get(nm); if (cons == null) throw (new Resource.LoadException("Unknown material part name: " + nm, res)); ret.left.add(cons.cons(res, args)); } } return (ret); }
public void draw(GOut g) { g.chcolor(24, 24, 16, 200); g.frect(Coord.z, sz); g.chcolor(); int y = 0; boolean sel = false; synchronized (msgs) { for (Message msg : msgs) { if ((selstart != null) && (msg == selstart.msg)) sel = true; int y1 = y - sb.val; int y2 = y1 + msg.sz().y; if ((y2 > 0) && (y1 < ih())) { if (sel) drawsel(g, msg, y1); g.image(msg.tex(), new Coord(0, y1)); } if ((selend != null) && (msg == selend.msg)) sel = false; y += msg.sz().y; } } sb.max = y - ih(); super.draw(g); }
public void mapdata2(Message msg) { Coord c = msg.coord(); synchronized (grids) { synchronized (req) { if (req.containsKey(c)) { Grid g = grids.get(c); if (g == null) grids.put(c, g = new Grid(c)); g.fill(msg); req.remove(c); olseq++; } } } }
public Sprite create(Owner owner, Resource res, Message sdt) { int m = sdt.uint8(); GrowingPlant spr = new GrowingPlant(owner, res); spr.addnegative(); Random rnd = owner.mkrandoom(); int n = Config.simple_plants ? 1 : num; for (int i = 0; i < n; i++) { Coord c; if (Config.simple_plants) { c = neg.bc.add(neg.bs).sub(5, 5); } else { c = new Coord(rnd.nextInt(neg.bs.x), rnd.nextInt(neg.bs.y)).add(neg.bc); } Tex s = strands[m][rnd.nextInt(strands[m].length)]; spr.add(s, 0, MapView.m2s(c), new Coord(s.sz().x / 2, s.sz().y).inv()); } return (spr); }
public void init() { if (spr != null) return; spr = Sprite.create(gob, res.get(), sdt.clone()); }
public Res cons(final Resource res, Message buf) { int id = buf.uint16(); Res ret = new Res(res, id); GLState light = Light.deflight; while (!buf.eom()) { String thing = buf.string().intern(); if (thing == "col") { Color amb = col(buf); Color dif = col(buf); Color spc = col(buf); double shine = buf.cpfloat(); Color emi = col(buf); ret.states.add(new Colors(amb, dif, spc, emi, (float) shine)); } else if (thing == "linear") { ret.linear = true; } else if (thing == "mipmap") { ret.mipmap = true; } else if (thing == "nofacecull") { ret.states.add(nofacecull); } else if (thing == "tex") { final int tid = buf.uint16(); ret.left.add( new Res.Resolver() { public void resolve(Collection<GLState> buf) { for (Resource.Image img : res.layers(Resource.imgc)) { if (img.id == tid) { buf.add(img.tex().draw()); buf.add(img.tex().clip()); return; } } throw (new RuntimeException( String.format("Specified texture %d not found in %s", tid, res))); } }); } else if (thing == "texlink") { final String nm = buf.string(); final int ver = buf.uint16(); final int tid = buf.uint16(); ret.left.add( new Res.Resolver() { public void resolve(Collection<GLState> buf) { Indir<Resource> tres = res.pool.load(nm, ver); for (Resource.Image img : tres.get().layers(Resource.imgc)) { if (img.id == tid) { buf.add(img.tex().draw()); buf.add(img.tex().clip()); return; } } throw (new RuntimeException( String.format( "Specified texture %d for %s not found in %s", tid, res, tres))); } }); } else if (thing == "light") { String l = buf.string(); if (l.equals("pv")) { light = Light.vlights; } else if (l.equals("pp")) { light = Light.plights; } else if (l.equals("n")) { light = null; } else { throw (new Resource.LoadException("Unknown lighting type: " + thing, res)); } } else { throw (new Resource.LoadException("Unknown material part: " + thing, res)); } } if (light != null) ret.states.add(light); return (ret); }
private ByRes(Resource res, Message buf, Map<Integer, Factory> refs) { String resnm = buf.string(); int resver = buf.uint16(); this.res = res.pool.load(resnm, resver); }
public void fill(Message msg) { String mmname = msg.string().intern(); if (mmname.equals("")) mnm = null; else mnm = mmname; int[] pfl = new int[256]; while (true) { int pidx = msg.uint8(); if (pidx == 255) break; pfl[pidx] = msg.uint8(); } Message blob = msg.inflate(); id = blob.int64(); for (int i = 0; i < tiles.length; i++) tiles[i] = blob.uint8(); for (int i = 0; i < z.length; i++) z[i] = blob.int16(); for (int i = 0; i < ol.length; i++) ol[i] = 0; while (true) { int pidx = blob.uint8(); if (pidx == 255) break; int fl = pfl[pidx]; int type = blob.uint8(); Coord c1 = new Coord(blob.uint8(), blob.uint8()); Coord c2 = new Coord(blob.uint8(), blob.uint8()); int ol; if (type == 0) { if ((fl & 1) == 1) ol = 2; else ol = 1; } else if (type == 1) { if ((fl & 1) == 1) ol = 8; else ol = 4; } else { throw (new RuntimeException("Unknown plot type " + type)); } for (int y = c1.y; y <= c2.y; y++) { for (int x = c1.x; x <= c2.x; x++) { this.ol[x + (y * cmaps.x)] |= ol; } } } invalidate(); }