Skip to content

Commit

Permalink
feat(ntv): add stubs for all unimplemented natives; implement a few o…
Browse files Browse the repository at this point in the history
…f them
  • Loading branch information
theY4Kman committed Mar 14, 2023
1 parent 8afdb7a commit 9b826e7
Show file tree
Hide file tree
Showing 71 changed files with 7,375 additions and 37 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
### Added
- Add `GetMyHandle`, `GetPluginFilename`, and `GetPluginInfo` natives
- Add random natives `SetURandomSeed`, `GetURandomInt`, and `GetURandomFloat`
- Add `Pointer` and `Array` native param type annotations
- Add natives/methodmap/enum stub generation utility (install with `stubgen` extra; py3.11+ only; run with `pysmx_stubgen <output-directory>`)
- Add stubs for all unimplemented natives

### Changed
- Switch `@native` decorator to interpret param types from typing annotations
Expand Down
6 changes: 5 additions & 1 deletion pysmx_stubgen/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def param_decl_new(self, children):
else:
name = str(children[i])
i += 1
if isinstance(children[i], list):
if children[i] and isinstance(children[i], list):
dims = children[i]
i += 1

Expand All @@ -126,6 +126,10 @@ def param_decl_new(self, children):
def type_dims(self, children):
return [None] * len(children)

def old_dims(self, children):
if children:
return [child or None for child in children]


class EnumTransformer(BaseTransformer):
def enum_items(self, children):
Expand Down
3 changes: 2 additions & 1 deletion pysmx_stubgen/symbols.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def __bool__(self):

@property
def py_mod(self) -> ImportedName:
return _import(f'smx.natives.{self.name}')
return _import(f'smx.sourcemod.natives.{self.name}')

def py_format(
self,
Expand Down Expand Up @@ -482,6 +482,7 @@ def py_format(
import_section = isort.code(
import_section,
multi_line_output=WrapModes.VERTICAL_HANGING_INDENT,
include_trailing_comma=True,
)

sections.insert(0, import_section.strip())
Expand Down
11 changes: 10 additions & 1 deletion smx/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,19 @@ class ParamInfo:
P = ParamSpec('P')


class CallableReturnValue(NamedTuple, Generic[RV]):
@dataclasses.dataclass
class CallableReturnValue(Generic[RV]):
rval: RV
args: List[ParamValueT]

def __getitem__(self, item):
if item == 0:
return self.rval
elif item == 1:
return self.args
else:
raise IndexError(item)


class ICallable(Generic[P, RV]):
def push_cell(self, value: int):
Expand Down
13 changes: 12 additions & 1 deletion smx/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ctypes import addressof, sizeof
from datetime import datetime
from pathlib import Path
from random import Random
from typing import Any, cast as typing_cast, Dict, Generic, List, Tuple, TYPE_CHECKING, TypeVar

from smx.compat import ParamSpec
Expand All @@ -18,7 +19,6 @@
ParamValueT,
)
from smx.rtti import RTTI
from smx.sourcemod.natives.base import convert_return_value, WritableString
from smx.vm import SourcePawnAbstractMachine

if TYPE_CHECKING:
Expand Down Expand Up @@ -72,6 +72,15 @@ def __init__(
self.console: List[Tuple[datetime, str]] = []
self.console_redirect = sys.stdout

# Per-plugin random number generator
self._rand: Random | None = None

@property
def rand(self) -> Random:
if self._rand is None:
self._rand = Random()
return self._rand

def printf(self, msg) -> None:
self.console.append((datetime.now(), msg))
if self.console_redirect is not None:
Expand Down Expand Up @@ -330,6 +339,8 @@ def invoke(self) -> Tuple[CallableReturnValue[RV] | None, bool]:
# TODO(zk): check runnable
# TODO(zk): check error state

from smx.sourcemod.natives.base import convert_return_value, WritableString

# Copy params, allowing reentrancy (calls within calls, yo)
params = deepcopy(self._params)

Expand Down
3 changes: 3 additions & 0 deletions smx/sourcemod/handles.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def __init__(self, sys):
def __getitem__(self, handle_id: int):
return self._handles[handle_id].obj

def __contains__(self, item):
return item in self._handles

def get_raw(self, handle_id: int) -> SourceModHandle | None:
return self._handles.get(handle_id)

Expand Down
125 changes: 116 additions & 9 deletions smx/sourcemod/natives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,69 @@
from pathlib import Path
from typing import Any, Callable, Dict, List, Tuple, TYPE_CHECKING

from smx.sourcemod.natives.admin import AdminNatives
from smx.sourcemod.natives.adminmenu import AdminmenuNatives
from smx.sourcemod.natives.adt_array import AdtArrayNatives
from smx.sourcemod.natives.adt_stack import AdtStackNatives
from smx.sourcemod.natives.adt_trie import AdtTrieNatives
from smx.sourcemod.natives.banning import BanningNatives
from smx.sourcemod.natives.base import MethodMap
from smx.sourcemod.natives.console import ConsoleNatives
from smx.sourcemod.natives.convars import ConVarNatives
from smx.sourcemod.natives.files import FilesNatives
from smx.sourcemod.natives.float import FloatNatives
from smx.sourcemod.natives.handles import HandlesNatives
from smx.sourcemod.natives.shell import ShellNatives
from smx.sourcemod.natives.basecomm import BasecommNatives
from smx.sourcemod.natives.bitbuffer import BitbufferNatives
from smx.sourcemod.natives.clientprefs import ClientprefsNatives
from smx.sourcemod.natives.clients import ClientsNatives
from smx.sourcemod.natives.commandfilters import CommandfiltersNatives
from smx.sourcemod.natives.commandline import CommandlineNatives
from smx.sourcemod.natives.console import ConsoleNatives, ConsoleNatives
from smx.sourcemod.natives.convars import ConvarsNatives
from smx.sourcemod.natives.core import CoreNatives
from smx.sourcemod.natives.cstrike import CstrikeNatives
from smx.sourcemod.natives.datapack import DatapackNatives
from smx.sourcemod.natives.dbi import DbiNatives
from smx.sourcemod.natives.dhooks import DhooksNatives
from smx.sourcemod.natives.entity import EntityNatives
from smx.sourcemod.natives.entity_prop_stocks import EntityPropStocksNatives
from smx.sourcemod.natives.entitylump import EntitylumpNatives
from smx.sourcemod.natives.events import EventsNatives
from smx.sourcemod.natives.files import FilesNatives, FilesNatives
from smx.sourcemod.natives.float import FloatNatives, FloatNatives
from smx.sourcemod.natives.functions import FunctionsNatives
from smx.sourcemod.natives.geoip import GeoipNatives
from smx.sourcemod.natives.halflife import HalflifeNatives
from smx.sourcemod.natives.handles import HandlesNatives, HandlesNatives
from smx.sourcemod.natives.keyvalues import KeyvaluesNatives
from smx.sourcemod.natives.lang import LangNatives
from smx.sourcemod.natives.logging import LoggingNatives
from smx.sourcemod.natives.mapchooser import MapchooserNatives
from smx.sourcemod.natives.menus import MenusNatives
from smx.sourcemod.natives.nextmap import NextmapNatives
from smx.sourcemod.natives.profiler import ProfilerNatives
from smx.sourcemod.natives.protobuf import ProtobufNatives
from smx.sourcemod.natives.regex import RegexNatives
from smx.sourcemod.natives.sdkhooks import SdkhooksNatives
from smx.sourcemod.natives.sdktools import SdktoolsNatives
from smx.sourcemod.natives.sdktools_client import SdktoolsClientNatives
from smx.sourcemod.natives.sdktools_engine import SdktoolsEngineNatives
from smx.sourcemod.natives.sdktools_entinput import SdktoolsEntinputNatives
from smx.sourcemod.natives.sdktools_entoutput import SdktoolsEntoutputNatives
from smx.sourcemod.natives.sdktools_functions import SdktoolsFunctionsNatives
from smx.sourcemod.natives.sdktools_gamerules import SdktoolsGamerulesNatives
from smx.sourcemod.natives.sdktools_sound import SdktoolsSoundNatives
from smx.sourcemod.natives.sdktools_stringtables import SdktoolsStringtablesNatives
from smx.sourcemod.natives.sdktools_tempents import SdktoolsTempentsNatives
from smx.sourcemod.natives.sdktools_trace import SdktoolsTraceNatives
from smx.sourcemod.natives.sdktools_variant_t import SdktoolsVariantTNatives
from smx.sourcemod.natives.sdktools_voice import SdktoolsVoiceNatives
from smx.sourcemod.natives.shell import ShellNatives, ShellNatives
from smx.sourcemod.natives.sorting import SortingNatives
from smx.sourcemod.natives.sourcemod import SourceModIncNatives
from smx.sourcemod.natives.string import StringNatives
from smx.sourcemod.natives.timers import TimerNatives
from smx.sourcemod.natives.string import StringNatives, StringNatives
from smx.sourcemod.natives.textparse import TextparseNatives
from smx.sourcemod.natives.tf2 import Tf2Natives
from smx.sourcemod.natives.timers import TimerNatives, TimerNatives
from smx.sourcemod.natives.topmenus import TopmenusNatives
from smx.sourcemod.natives.usermessages import UsermessagesNatives
from smx.sourcemod.natives.vector import VectorNatives

if TYPE_CHECKING:
from smx.runtime import SourcePawnPluginRuntime
Expand Down Expand Up @@ -88,14 +141,68 @@ def _read_include_file_natives(include_dir: str | Path | None = None) -> List[Tu
class SourceModNatives(
BaseSourceModNatives,

AdminmenuNatives,
AdminNatives,
AdtArrayNatives,
AdtStackNatives,
AdtTrieNatives,
BanningNatives,
BasecommNatives,
BitbufferNatives,
ClientprefsNatives,
ClientsNatives,
CommandfiltersNatives,
CommandlineNatives,
ConsoleNatives,
ConVarNatives,
ConvarsNatives,
CoreNatives,
CstrikeNatives,
DatapackNatives,
DbiNatives,
DhooksNatives,
EntitylumpNatives,
EntityNatives,
EntityPropStocksNatives,
EventsNatives,
FilesNatives,
FloatNatives,
FunctionsNatives,
GeoipNatives,
HalflifeNatives,
HandlesNatives,
KeyvaluesNatives,
LangNatives,
LoggingNatives,
MapchooserNatives,
MenusNatives,
NextmapNatives,
ProfilerNatives,
ProtobufNatives,
RegexNatives,
SdkhooksNatives,
SdktoolsClientNatives,
SdktoolsEngineNatives,
SdktoolsEntinputNatives,
SdktoolsEntoutputNatives,
SdktoolsFunctionsNatives,
SdktoolsGamerulesNatives,
SdktoolsNatives,
SdktoolsSoundNatives,
SdktoolsStringtablesNatives,
SdktoolsTempentsNatives,
SdktoolsTraceNatives,
SdktoolsVariantTNatives,
SdktoolsVoiceNatives,
ShellNatives,
SortingNatives,
SourceModIncNatives,
StringNatives,
TextparseNatives,
Tf2Natives,
TimerNatives,
TopmenusNatives,
UsermessagesNatives,
VectorNatives,
):
pass

Expand Down
Loading

0 comments on commit 9b826e7

Please sign in to comment.