Jump to content
Sign in to follow this  
MatroseFuchs

ModAPI (discussion)

2 comments in this topic

Recommended Posts

WG Staff
3 posts
17 battles

All greetings!

We updated the ModAPI documentation on our forum, added descriptions, new methods and sections, as well as updated instructions for creating mods.

https://forum.worldofwarships.ru/topic/69084-modapi-документация/

https://forum.worldofwarships.ru/topic/64856-modsapi-how-to/

Similar topics in the American forum will be added later. We are currently translating the documentation.

Share this post


Link to post
Share on other sites
[LBAS]
Member
4,501 posts
25,682 battles

I need WG to support my custom ports loader (xml)

its 2 years old and I want it becomes official support with "xml",

just like before, easy and friendly to use it

Spoiler
Quote

API_VERSION = 'API_v1.0'

MOD_VERSION = 'v1.3'

def log(message): print('[CustomPortsLoader] %s' % (message))
def error(message): print('[CustomPortsLoader][E] %s' % (message))
def debug(message): print('[CustomPortsLoader][D] %s' % (message))

# CustomPortsLoader version 1.3

log('Loading ports...')
debug('gamever %s' % (utils.getGameVersion()))
debug('apiver %s' % (API_VERSION))
debug('modver %s' % (MOD_VERSION))

import xml

ET = xml.etree.ElementTree

# <port name="PortName" display="Port Display Name" barge="true" bargePosX="100" bargePosY="100" bargePosZ="100" bargeYaw="55.0" bargeWaypointX="0" bargeWaypointY="0" bargeWaypointZ="0"/>

DISPLAY_NAME_DEFAULT = 'Custom Port'
BARGEPOS_DEFAULT = False

def map(fn, it):
  for i in range(0, len(it)):
    it = fn(it)
  return it

LOADED_PORTS = []

def unload():
  log('Unloading ports...')
  for port in LOADED_PORTS:
    log('Unloading %s...' % (port))
    try:
      customPorts.removeCustomPort(port)
      LOADED_PORTS.remove(port)
    except: error('Could not unload port %s!' % (port))

def load():
  log('Loading ports...')
  for root, dirs, files in utils.walk('.'):
    for file in files:
      filename = '%s/%s' % (root, file)
      ext = file.split('.')[-1].lower()
      if ext != 'xml': continue
      try:
        log('Reading XML %s for port info...' % (file))
        with open(filename, 'r') as xml_f:
          xml = ET.fromstring(xml_f.read()).getchildren()[0]
          if xml.tag != 'ports':
            error('Malformed xml, no <ports> tag')
            continue
          for port in xml.getchildren():
            if port.tag == 'port':
              if 'name' not in port.attrib:
                error('Malformed xml, no name attribute for <port> tag')
                break
              name = port.attrib['name']
              display = DISPLAY_NAME_DEFAULT
              if 'display' in port.attrib: display = port.attrib['display']
              
              bargePos = BARGEPOS_DEFAULT
              for child in port.getchildren():
                if child.tag == 'barge':
                  bargePos = [[0, 0, 0], 0, [0, 0, 0]]

                  if 'x' in child.attrib: bargePos[0][0] = float(child.attrib['x'])
                  if 'y' in child.attrib: bargePos[0][1] = float(child.attrib['y'])
                  if 'z' in child.attrib: bargePos[0][2] = float(child.attrib['z'])

                  if 'yaw' in child.attrib: bargePos[1] = float(child.attrib['yaw'])
                  
                  if 'waypointX' in child.attrib: bargePos[2][0] = float(child.attrib['waypointX'])
                  if 'waypointY' in child.attrib: bargePos[2][1] = float(child.attrib['waypointY'])
                  if 'waypointZ' in child.attrib: bargePos[2][1] = float(child.attrib['waypointZ'])

                  bargePos = ((bargePos[0][0], bargePos[0][1], bargePos[0][2]), bargePos[1], (bargePos[2][0], bargePos[2][1], bargePos[2][2]))

              customPorts.addCustomPort(name, portDisplayName = display, bargePos = bargePos)
              LOADED_PORTS.append(name)
              log('Added port %s with name %s (bargePos: %s)' % (name, display, bargePos))
      except: error('Unexpected exception with file...')  
      #finally: pass

load()

import Keys

def keyEvent(event):
  if event.isAltDown() and event.isKeyDown() and event.key == Keys.KEY_F10:
    log('Reloading ports...')
    unload()
    load()
    return True
  return False

events.onKeyEvent(keyEvent)

 

 

and this is how it look like

1 folder 1 mod multi-ports

Untitled.jpg

Edited by MatterCore

Share this post


Link to post
Share on other sites
Sign in to follow this  

×