/** Cleans snapshots of entries over 60 seconds old (executed every second) */
 public void cleanSnapshotDeque() {
   long curTime = GameEngine.getTimestamp(); // We need to compare
   // timestamps
   if (curTime - lastCleanedChatlogs > 1000) { // Every second
     lastCleanedChatlogs = curTime;
     lastCleanedChatlogsOutput++;
     if (lastCleanedChatlogsOutput > 60 * 5) {
       Logger.println("----------------------------------------------");
       Logger.println(world.getSnapshots().size() + " items on deque");
     }
     Iterator<Snapshot> i = world.getSnapshots().descendingIterator();
     Snapshot s = null;
     while (i.hasNext()) {
       s = i.next();
       if (curTime - s.getTimestamp() > 60000) {
         i.remove();
         s = null;
       } else {
         s = null;
       }
     }
     i = null;
     if (lastCleanedChatlogsOutput > 60 * 5) {
       Logger.println(world.getSnapshots().size() + " items on deque AFTER CLEANUP");
       Logger.println("----------------------------------------------");
       lastCleanedChatlogsOutput = 0;
     }
   }
 }
 /** Cleans garbage (Tilecleanup) */
 public synchronized void garbageCollect() {
   long startTime = getTime();
   int curMemory =
       (int) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000;
   for (int i = 0; i < Instance.getWorld().tiles.length; i++) {
     for (int in = 0; in < Instance.getWorld().tiles[i].length; in++) {
       ActiveTile tile = Instance.getWorld().tiles[i][in];
       if (tile != null) {
         if (!tile.hasGameObject() && !tile.hasItems() && !tile.hasNpcs() && !tile.hasPlayers()) {
           Instance.getWorld().tiles[i][in] = null;
         }
       }
     }
   }
   Runtime.getRuntime().gc();
   int newMemory =
       (int) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1000;
   Logger.println("GARBAGE COLLECT | Executing Memory Cleanup");
   Logger.println(
       "GARBAGE COLLECT | Memory before: "
           + curMemory
           + "kb"
           + " Memory after: "
           + newMemory
           + " (Freed: "
           + (curMemory - newMemory)
           + "kb)");
   Logger.println("GARBAGE COLLECT | Cleanup took " + (updateTime() - startTime) + "ms");
 }
  private void processIncomingPackets() {
    for (RSCPacket p : packetQueue.getPackets()) {
      IoSession session = p.getSession();
      Player player = (Player) session.getAttachment();
      if (player.getUsername() == null && p.getID() != 32 && p.getID() != 77 && p.getID() != 0) {
        final String ip = player.getCurrentIP();
        IPBanManager.throttle(ip);
        continue;
      }
      PacketHandler handler = packetHandlers.get(p.getID());
      player.ping();
      if (handler != null) {
        try {
          handler.handlePacket(p, session);
          try {
            if (p.getID() != 5) {
              // String s = "[PACKET] " +
              // session.getRemoteAddress().toString().replace("/",
              // "") + " : " + p.getID()+
              // " ["+handler.getClass().toString()+"]" + " : "+
              // player.getUsername() + " : ";
              // for(Byte b : p.getData())
              // s += b;
              // Logger.println(s);
            }
          } catch (Exception e) {
            e.printStackTrace();
          }

        } catch (Exception e) {
          String s;
          StringWriter sw = new StringWriter();
          PrintWriter pw = new PrintWriter(sw, true);
          e.printStackTrace(pw);
          pw.flush();
          sw.flush();
          s = sw.toString();
          Logger.error(
              "Exception with p["
                  + p.getID()
                  + "] from "
                  + player.getUsername()
                  + " ["
                  + player.getCurrentIP()
                  + "]: "
                  + s);
          player.getActionSender().sendLogout();
          player.destroy(false);
        }
      } else {
        Logger.error(
            "Unhandled packet from "
                + player.getCurrentIP()
                + ": "
                + p.getID()
                + "len: "
                + p.getLength());
      }
    }
  }
Example #4
0
 @SuppressWarnings("unchecked")
 public void loadWorld(World world) {
   try {
     tileArchive = new ZipFile(new File(Config.CONF_DIR, "data/Landscape.rscd"));
     // out = new ZipOutputStream(new FileOutputStream(new
     // File(Config.CONF_DIR, "data/new_Landscape.rscd")));
     // out.setLevel(9);
   } catch (Exception e) {
     Logger.error(e);
   }
   long now = System.currentTimeMillis();
   for (int lvl = 0; lvl < 4; lvl++) {
     int wildX = 2304;
     int wildY = 1776 - (lvl * 944);
     for (int sx = 0; sx < 1000; sx += 48) {
       for (int sy = 0; sy < 1000; sy += 48) {
         int x = (sx + wildX) / 48;
         int y = (sy + (lvl * 944) + wildY) / 48;
         loadSection(x, y, lvl, world, sx, sy + (944 * lvl));
       }
     }
   }
   System.out.println((System.currentTimeMillis() - now) / 1000 + "s to parse");
   // try { out.close(); } catch(Exception e) { Logger.error(e); }
   for (Shop shop : (List<Shop>) PersistenceManager.load("locs/Shops.xml.gz")) {
     world.registerShop(shop);
   }
   System.gc();
 }
  public void sessionCreated(IoSession session) {
    session
        .getFilterChain()
        .addFirst("protocolFilter", new ProtocolCodecFilter(new RSCCodecFactory()));

    Logger.println(
        "Connection from: "
            + ((InetSocketAddress) session.getRemoteAddress()).getAddress().getHostAddress());
  }
 /**
  * Loads the packet handling classes from the persistence manager.
  *
  * @throws Exception
  */
 protected void loadPacketHandlers() throws Exception {
   PacketHandlerDef[] handlerDefs = Instance.dataStore().loadPacketHandlerDefs();
   for (PacketHandlerDef handlerDef : handlerDefs) {
     try {
       String className = handlerDef.getClassName();
       Class<?> c = Class.forName(className);
       if (c != null) {
         PacketHandler handler = (PacketHandler) c.newInstance();
         for (int packetID : handlerDef.getAssociatedPackets()) {
           packetHandlers.put(packetID, handler);
         }
       }
     } catch (Exception e) {
       Logger.error(e);
     }
   }
 }
  /** The thread execution process. */
  public void run() {
    Logger.println("GameEngine now running");
    time = System.nanoTime() / 1000000000;

    eventHandler.add(
        new DelayedEvent(null, Config.GARBAGE_COLLECT_INTERVAL) { // Ran
          // every
          // 50*2
          // minutes
          @Override
          public void run() {
            new Thread(
                    new Runnable() {
                      public void run() {
                        garbageCollect();
                      }
                    })
                .start();
          }
        });
    eventHandler.add(
        new DelayedEvent(null, Config.SAVE_INTERVAL) {
          public void run() {
            long now = GameEngine.getTime();
            for (Player p : world.getPlayers()) {
              if (now - p.getLastSaveTime() >= Config.SAVE_INTERVAL) {
                p.save();
                p.setLastSaveTime(now);
              }
            }
            Instance.getServer().getLoginConnector().getActionSender().saveProfiles();
          }
        });
    while (running) {
      try {
        Thread.sleep(50);
      } catch (InterruptedException ie) {
      }
      long deltaTime = updateTime();
      processLoginServer();
      if ((deltaTime = getDeltaTime()) >= 1000)
        Logger.println(
            "processLoginServer is taking longer than it should, exactly " + deltaTime + "ms");
      processIncomingPackets();
      if ((deltaTime = getDeltaTime()) >= 1000)
        Logger.println(
            "processIncomingPackets is taking longer than it should, exactly " + deltaTime + "ms");
      processEvents();
      if ((deltaTime = getDeltaTime()) >= 1000)
        Logger.println(
            "processEvents is taking longer than it should, exactly " + deltaTime + "ms");
      processClients();
      if ((deltaTime = getDeltaTime()) >= 1000)
        Logger.println(
            "processClients is taking longer than it should, exactly " + deltaTime + "ms");
      cleanSnapshotDeque();
      if ((deltaTime = getDeltaTime()) >= 1000)
        Logger.println(
            "processSnapshotDeque is taking longer than it should, exactly " + deltaTime + "ms");
    }
  }
 public void kill() {
   Logger.println("Terminating GameEngine");
   running = false;
 }
Example #9
0
  private void loadSection(
      int sectionX, int sectionY, int height, World world, int bigX, int bigY) {
    // System.out.println(1);
    Sector s = null;
    try {
      String filename = "h" + height + "x" + sectionX + "y" + sectionY;
      ZipEntry e = tileArchive.getEntry(filename);
      if (e == null) {
        throw new Exception("Missing tile: " + filename);
      }
      ByteBuffer data =
          DataConversions.streamToBuffer(new BufferedInputStream(tileArchive.getInputStream(e)));
      s = Sector.unpack(data);
      // s = modifyAndSave(filename, s, bigX, bigY);
    } catch (Exception e) {
      Logger.error(e);
    }
    // System.out.println(2);
    for (int y = 0; y < Sector.HEIGHT; y++) {
      for (int x = 0; x < Sector.WIDTH; x++) {
        int bx = bigX + x;
        int by = bigY + y;
        if (!world.withinWorld(bx, by)) {
          continue;
        }

        world.getTileValue(bx, by).overlay = s.getTile(x, y).groundOverlay;
        world.getTileValue(bx, by).diagWallVal = s.getTile(x, y).diagonalWalls;
        world.getTileValue(bx, by).horizontalWallVal = s.getTile(x, y).horizontalWall;
        world.getTileValue(bx, by).verticalWallVal = s.getTile(x, y).verticalWall;
        world.getTileValue(bx, by).elevation = s.getTile(x, y).groundElevation;
        /** start of shit * */
        if ((s.getTile(x, y).groundOverlay & 0xff) == 250) {
          s.getTile(x, y).groundOverlay = (byte) 2;
        }
        /** break in shit * */
        int groundOverlay = s.getTile(x, y).groundOverlay & 0xFF;
        if (groundOverlay > 0 && EntityHandler.getTileDef(groundOverlay - 1).getObjectType() != 0) {
          world.getTileValue(bx, by).mapValue |= 0x40; // 64
        }

        int verticalWall = s.getTile(x, y).verticalWall & 0xFF;
        if (verticalWall > 0
            && EntityHandler.getDoorDef(verticalWall - 1).getUnknown() == 0
            && EntityHandler.getDoorDef(verticalWall - 1).getDoorType() != 0) {
          world.getTileValue(bx, by).mapValue |= 1; // 1
          world.getTileValue(bx, by - 1).mapValue |= 4; // 4
        }

        int horizontalWall = s.getTile(x, y).horizontalWall & 0xFF;
        if (horizontalWall > 0
            && EntityHandler.getDoorDef(horizontalWall - 1).getUnknown() == 0
            && EntityHandler.getDoorDef(horizontalWall - 1).getDoorType() != 0) {
          world.getTileValue(bx, by).mapValue |= 2; // 2
          world.getTileValue(bx - 1, by).mapValue |= 8; // 8
        }

        int diagonalWalls = s.getTile(x, y).diagonalWalls;
        if (diagonalWalls > 0
            && diagonalWalls < 12000
            && EntityHandler.getDoorDef(diagonalWalls - 1).getUnknown() == 0
            && EntityHandler.getDoorDef(diagonalWalls - 1).getDoorType() != 0) {
          world.getTileValue(bx, by).mapValue |= 0x20; // 32
        }
        if (diagonalWalls > 12000
            && diagonalWalls < 24000
            && EntityHandler.getDoorDef(diagonalWalls - 12001).getUnknown() == 0
            && EntityHandler.getDoorDef(diagonalWalls - 12001).getDoorType() != 0) {
          world.getTileValue(bx, by).mapValue |= 0x10; // 16
        }
        /** end of shit * */
      }
    }
    // System.out.println(3);
  }