Skip to content

Commit

Permalink
Swift 5.9 updates
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfairh committed Jun 25, 2023
1 parent b7a0c59 commit f57e115
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 151 deletions.
108 changes: 0 additions & 108 deletions .swiftpm/xcode/xcshareddata/xcschemes/spacewar-swift.xcscheme

This file was deleted.

12 changes: 6 additions & 6 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/johnfairh/steamworks-swift",
"state" : {
"branch" : "main",
"revision" : "78fae74c61a7e91096b4beecb01a18e72b562a05"
"revision" : "d65d442139b042ce1172de95b915e9974c7d7fa7",
"version" : "0.3.0"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "6fe203dc33195667ce1759bf0182975e4653ba1c",
"version" : "1.4.4"
"revision" : "32e8d724467f8fe623624570367e3d50c5638e46",
"version" : "1.5.2"
}
},
{
"identity" : "tmlengines",
"kind" : "remoteSourceControl",
"location" : "https://github.com/johnfairh/TMLEngines",
"state" : {
"revision" : "81646b99277ce42ee5acc34577965cc292c2f587",
"version" : "1.2.4"
"revision" : "51a2ff3056ff60d3ebc6df1f3d8504d02b7c8ef3",
"version" : "1.3.0"
}
}
],
Expand Down
9 changes: 5 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.9

import PackageDescription

let package = Package(
name: "spacewar-swift",
platforms: [
.macOS("12.0"),
.macOS("13.0"),
],
dependencies: [
.package(url: "https://github.com/johnfairh/steamworks-swift",
branch: "main"),
from: "0.3.0"),
.package(url: "https://github.com/johnfairh/TMLEngines",
from: "1.2.0")
],
Expand All @@ -27,7 +27,8 @@ let package = Package(
.process("Resources/steam_input_manifest.vdf"),
.process("Resources/xbox_controller.vdf"),
.process("Resources/ps5_controller.vdf")
]
],
swiftSettings: [.interoperabilityMode(.Cxx)]
),
.systemLibrary(name: "CSpaceWar")
]
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# spacewar-swift

Educational port of Steamworks demo to Swift for macOS with Metal backend.

Needs Xcode 15 / Swift 5.9

Needs Steam up and logged in; best run from CLI `swift run`.
46 changes: 28 additions & 18 deletions Sources/CSpaceWar/cspacewar.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#ifndef CSPACEWAR_H

extern "C" {

#include <string.h>

typedef short int16;
Expand All @@ -8,7 +10,7 @@ typedef int int32;
typedef unsigned int uint32;
typedef long long int64;
typedef unsigned long long uint64;
typedef unsigned char bool;
typedef unsigned char netbool;
typedef unsigned char uint8;
typedef uint32 netfloat; // network rep of single-prec floating-point

Expand All @@ -20,7 +22,7 @@ typedef uint32 netfloat; // network rep of single-prec floating-point
typedef struct {
uint32 messageType;
uint64 steamIDServer;
bool isVACSecure;
netbool isVACSecure;
char serverName[128];
} MsgServerSendInfo_t;

Expand Down Expand Up @@ -60,8 +62,14 @@ typedef struct {

#define ARRAY_GETTER(TYPE,FIELD,FIELDTYPE) \
__attribute__((swift_name("getter:" #TYPE "." #FIELD "_ptr(self:)"))) \
static inline const FIELDTYPE * _Nonnull TYPE ## _Get ## FIELD (const TYPE * _Nonnull t) { \
return &t->FIELD[0]; \
}

#define ARRAY_MUTABLE_GETTER(TYPE,FIELD,FIELDTYPE) \
__attribute__((swift_name("getter:" #TYPE "." #FIELD "_ptr(self:)"))) \
static inline FIELDTYPE * _Nonnull TYPE ## _Get ## FIELD (const TYPE * _Nonnull t) { \
return t->FIELD; \
return const_cast<FIELDTYPE *>(&t->FIELD[0]); \
}

ARRAY_GETTER(MsgClientBeginAuthentication_t, token, uint8)
Expand All @@ -76,11 +84,11 @@ static inline void MsgClientBeginAuthentication_SetToken(MsgClientBeginAuthentic
typedef struct {
uint32 messageType;
uint32 tokenLen;
char token[1024];
uint8 token[1024];
uint64 steamID;
} MsgP2PSendingTicket_t;

ARRAY_GETTER(MsgP2PSendingTicket_t, token, uint8)
ARRAY_MUTABLE_GETTER(MsgP2PSendingTicket_t, token, uint8)

typedef struct {
uint32 messageType;
Expand All @@ -89,13 +97,13 @@ typedef struct {
uint8 data[1024];
} MsgVoiceChatData_t;

ARRAY_GETTER(MsgVoiceChatData_t, data, uint8)
ARRAY_MUTABLE_GETTER(MsgVoiceChatData_t, data, uint8)

// MARK: Game, Server -> Client

typedef struct {
// Does the photon beam exist right now?
bool isActive;
netbool isActive;

// The current rotation
netfloat currentRotation;
Expand Down Expand Up @@ -132,14 +140,14 @@ typedef struct {
netfloat yPosition;

// Is the ship exploding?
bool exploding;
netbool exploding;

// Is the ship disabled?
bool disabled;
netbool disabled;

// Are the thrusters to be drawn?
bool forwardThrustersActive;
bool reverseThrustersActive;
netbool forwardThrustersActive;
netbool reverseThrustersActive;

// Decoration for this ship
int32 shipDecoration;
Expand Down Expand Up @@ -173,7 +181,7 @@ typedef struct {
uint32 playerWhoWonGame;

// which player slots are in use
bool playersActive[MAX_PLAYERS_PER_SERVER];
netbool playersActive[MAX_PLAYERS_PER_SERVER];

// what are the scores for each player?
uint32 playerScores[MAX_PLAYERS_PER_SERVER];
Expand All @@ -185,7 +193,7 @@ typedef struct {
uint64 playerSteamIDs[MAX_PLAYERS_PER_SERVER];
} ServerSpaceWarUpdateData_t;

ARRAY_GETTER(ServerSpaceWarUpdateData_t, playersActive, bool)
ARRAY_GETTER(ServerSpaceWarUpdateData_t, playersActive, netbool)
ARRAY_GETTER(ServerSpaceWarUpdateData_t, playerScores, uint32)
ARRAY_GETTER(ServerSpaceWarUpdateData_t, shipData, ServerShipUpdateData_t)
ARRAY_GETTER(ServerSpaceWarUpdateData_t, playerSteamIDs, uint64)
Expand All @@ -199,11 +207,11 @@ typedef struct {

typedef struct {
// Key's which are done
bool firePressed;
bool turnLeftPressed;
bool turnRightPressed;
bool forwardThrustersPressed;
bool reverseThrustersPressed;
netbool firePressed;
netbool turnLeftPressed;
netbool turnRightPressed;
netbool forwardThrustersPressed;
netbool reverseThrustersPressed;

// Decoration for this ship
int32 shipDecoration;
Expand Down Expand Up @@ -240,4 +248,6 @@ typedef struct {

#pragma pack( pop )

}

#endif
16 changes: 8 additions & 8 deletions Sources/SpaceWar/Messages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ extension Bool {

/// Fixed-size array tuple bullshit. holy fuck.
extension Array {
static func four<X>(_ p: UnsafeMutablePointer<X>, map: (X) -> Element) -> Array<Element>{
let bp = UnsafeMutableBufferPointer(start: p, count: 4)
static func four<X>(_ p: UnsafePointer<X>, map: (X) -> Element) -> Array<Element>{
let bp = UnsafeBufferPointer(start: p, count: 4)
return bp.map(map)
}

static func seven<X>(_ p: UnsafeMutablePointer<X>, map: (X) -> Element) -> Array<Element>{
let bp = UnsafeMutableBufferPointer(start: p, count: 7)
static func seven<X>(_ p: UnsafePointer<X>, map: (X) -> Element) -> Array<Element>{
let bp = UnsafeBufferPointer(start: p, count: 7)
return bp.map(map)
}

Expand Down Expand Up @@ -623,10 +623,10 @@ extension MsgP2PSendingTicket_t: ConstructableFrom {
if let buf = from.buffer {
precondition(buf.count <= 1024)
self.tokenLen = UInt32(buf.count).bigEndian
self.token_ptr.assign(from: buf.baseAddress!, count: buf.count)
self.token_ptr.update(from: buf.baseAddress!, count: buf.count)
} else {
self.tokenLen = UInt32(from.token.count).bigEndian
self.token_ptr.assign(from: from.token, count: from.token.count)
self.token_ptr.update(from: from.token, count: from.token.count)
}
}
}
Expand Down Expand Up @@ -665,11 +665,11 @@ extension MsgVoiceChatData_t: ConstructableFrom {
// got msg from a client, forwarding it...
precondition(buf.count <= 1024) /* ahem */
self.dataLength = UInt32(buf.count).bigEndian
self.data_ptr.assign(from: buf.baseAddress!, count: buf.count)
self.data_ptr.update(from: buf.baseAddress!, count: buf.count)
} else {
// created msg here, converting from swift
self.dataLength = UInt32(from.data.count).bigEndian
self.data_ptr.assign(from: from.data, count: from.data.count)
self.data_ptr.update(from: from.data, count: from.data.count)
}
}
}
Loading

0 comments on commit f57e115

Please sign in to comment.