Skip to content

Commit

Permalink
Add some Swift types for basic Swift engine SDK. Still WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
broken-bytes committed Jan 29, 2024
1 parent 41e755b commit dc9859f
Show file tree
Hide file tree
Showing 25 changed files with 269 additions and 26 deletions.
2 changes: 2 additions & 0 deletions editor/app/include/assets/AssetDatabase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <assetpackages/IAssetLoader.hxx>

#include <filesystem>
#include <map>
#include <memory>
#include <string>

Expand All @@ -23,6 +24,7 @@ namespace kyanite::editor {
auto Load(std::filesystem::path path) -> void;
auto GetModifiedTime(std::string uuid) -> std::time_t;
auto GetUuidForPath(std::string path) -> std::string;
auto LoadFileListForPackage(const assetpackages::AssetPackage* package) -> std::map<std::string, std::string> override;
auto LoadPackageList(std::string path) -> std::vector<assetpackages::AssetPackage*> override;
auto CheckIfPackageHasAsset(const assetpackages::AssetPackage* package, std::string path) -> bool override;
auto LoadPackage(std::string path) -> assetpackages::AssetPackage* override;
Expand Down
14 changes: 13 additions & 1 deletion editor/app/src/assets/AssetDatabase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,20 @@ namespace kyanite::editor {
return uuid;
}

auto AssetDatabase::LoadFileListForPackage(const assetpackages::AssetPackage* package) -> std::map<std::string, std::string> {
auto assets = GetAllAssets();
std::map<std::string, std::string> fileList;

for (auto& asset : assets) {
fileList.emplace(asset.uuid, asset.path);
}

return fileList;
}

auto AssetDatabase::LoadPackageList(std::string path)->std::vector<assetpackages::AssetPackage*> {
return {};
// The editor has no concept of packages, so we just return an empty vector
return { nullptr };
}

auto AssetDatabase::CheckIfPackageHasAsset(const assetpackages::AssetPackage* package, std::string path) -> bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace kyanite::engine::assetpackages {
AssetPackageLoader();
~AssetPackageLoader();

auto LoadFileListForPackage(const AssetPackage* package) -> std::map<std::string, std::string>;
auto LoadPackageList(std::string path) -> std::vector<AssetPackage*>;
auto CheckIfPackageHasAsset(const AssetPackage* package, std::string path) -> bool;
auto LoadPackage(std::string path) -> AssetPackage*;
Expand Down
14 changes: 11 additions & 3 deletions engine/assetpackages/include/assetpackages/AssetPackages.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <shared/Exported.hxx>
#include <shared/Serializable.hxx>

#include <map>
#include <memory>
#include <string>
#include <string_view>
Expand All @@ -19,13 +20,20 @@ namespace kyanite::engine::assetpackages {
*/
EXPORTED auto Initialize(std::shared_ptr<IAssetLoader> loader) -> void;

/**
@brief Loads a list of files from a directory
@param package The package
@return The mapping of the files to their UUIDs
*/
EXPORTED auto LoadFileListForPackage(const AssetPackage* package) -> std::map<std::string, std::string>;

/**
@brief Loads an asset from a file
@param path The path to the asset file
@return The loaded asset
@note This function is not type safe, and will return a void pointer. Used internally by the engine. Caller clears the memory
*/
auto LoadAssetFromPackage(const AssetPackage* package, std::string path) -> void*;
EXPORTED auto LoadAssetFromPackage(const AssetPackage* package, std::string uuid) -> void*;

/**
@brief Loads a package from a file
Expand All @@ -41,7 +49,7 @@ namespace kyanite::engine::assetpackages {
@param path The path to the asset
@return Whether the package has the asset
*/
EXPORTED auto PackageHasAsset(const AssetPackage* package, std::string path) -> bool;
EXPORTED auto PackageHasAsset(const AssetPackage* package, std::string uuid) -> bool;

/**
@brief Loads an asset from the package
Expand All @@ -50,7 +58,7 @@ namespace kyanite::engine::assetpackages {
@return The loaded asset
*/
template<typename T>
EXPORTED auto LoadAssetFromPackage(const AssetPackage* package, std::string path) -> T;
EXPORTED auto LoadAssetFromPackage(const AssetPackage* package, std::string uuid) -> T;

/**
@brief Creates a new package
Expand Down
2 changes: 2 additions & 0 deletions engine/assetpackages/include/assetpackages/IAssetLoader.hxx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <map>
#include <string>
#include <vector>

Expand All @@ -8,6 +9,7 @@ namespace kyanite::engine::assetpackages {

class IAssetLoader {
public:
virtual auto LoadFileListForPackage(const AssetPackage* package) -> std::map<std::string, std::string> = 0;
virtual auto LoadPackageList(std::string path) -> std::vector<AssetPackage*> = 0;
virtual auto CheckIfPackageHasAsset(const AssetPackage* package, std::string path) -> bool = 0;
virtual auto LoadPackage(std::string path) -> AssetPackage* = 0;
Expand Down
15 changes: 15 additions & 0 deletions engine/assetpackages/src/AssetPackageLoader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cereal/cereal.hpp>
#include <cereal/archives/binary.hpp>
#include <cereal/types/memory.hpp>
#include <cereal/types/map.hpp>
#include <cereal/types/string.hpp>
#include <cereal/types/vector.hpp>
#include <cereal/archives/json.hpp>
Expand All @@ -26,6 +27,20 @@ namespace kyanite::engine::assetpackages {

}

auto AssetPackageLoader::LoadFileListForPackage(const AssetPackage* package) -> std::map<std::string, std::string> {
auto mapping = core::LoadFileFromArchive(package->_path, "mapping.json");
std::stringstream ss;
ss.write(reinterpret_cast<const char*>(mapping.data()), mapping.size());

std::map<std::string, std::string> map;
{
cereal::JSONInputArchive archive(ss);
archive(map);
}

return map;
}

auto AssetPackageLoader::LoadPackageList(std::string path) -> std::vector<AssetPackage*> {
// Get all the files in the directory that end with .kpack and create a vector of AssetPackage pointers from them
std::vector<AssetPackage*> packages;
Expand Down
18 changes: 11 additions & 7 deletions engine/assetpackages/src/AssetPackages.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace kyanite::engine::assetpackages {
}
}

auto LoadFileListForPackage(const AssetPackage* package) -> std::map<std::string, std::string> {
return assetLoader->LoadFileListForPackage(package);
}

auto LoadPackage(const std::string path) -> AssetPackage* {
return assetLoader->LoadPackage(path);
}
Expand All @@ -32,18 +36,18 @@ namespace kyanite::engine::assetpackages {
return assetLoader->CheckIfPackageHasAsset(package, path);
}

template auto LoadAssetFromPackage(const AssetPackage* package, std::string path) -> audio::AudioClip;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string path) -> rendering::Material;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string path) -> rendering::Mesh;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string path) -> rendering::Shader;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string path) -> rendering::Texture;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string uuid) -> audio::AudioClip;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string uuid) -> rendering::Material;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string uuid) -> rendering::Mesh;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string uuid) -> rendering::Shader;
template auto LoadAssetFromPackage(const AssetPackage* package, std::string uuid) -> rendering::Texture;

template <typename T>
auto LoadAssetFromPackage(const AssetPackage* package, std::string path) -> T {
auto LoadAssetFromPackage(const AssetPackage* package, std::string uuid) -> T {
T data;

try {
auto asset = AssetPackageLoader().LoadAsset(package, path);
auto asset = AssetPackageLoader().LoadAsset(package, uuid);
std::stringstream stream(std::string(asset.begin(), asset.end()));
cereal::BinaryInputArchive archive(stream);
archive(data);
Expand Down
7 changes: 7 additions & 0 deletions scripting/engine/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ class Engine {
}

func start() {
let material = MaterialComponent()
print(material)
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(material)
print(String(data: data, encoding: .utf8)!)

while true {
var measure = ContinuousClock().measure {
// Attoseconds to milliseconds
Expand Down
8 changes: 5 additions & 3 deletions scripting/engine/Kyanite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@ public struct Kyanite {
public static func main() throws {
print("Hello World")
Bridge_Engine_Init(nil, nil, Mode(0), nil)
registerTransformComponent()
registerComponents()
let entity = Entity(name: "Test1")
entity.addComponent(TransformComponent())
let entity2 = Entity(name: "Test2")
entity2.addComponent(TransformComponent())

entity.addComponent(MaterialComponent())
let engine = Engine()
engine.start()
}

private static func registerTransformComponent() {
private static func registerComponents() {
_ComponentRegistry.shared._register(TransformComponent())
_ComponentRegistry.shared._register(MaterialComponent())
}
}

// Macros
@attached(member)
@attached(memberAttribute)
@attached(extension, conformances: Component, Hashable)
public macro Component() = #externalMacro(module: "Macros", type: "ComponentMacro")
3 changes: 3 additions & 0 deletions scripting/engine/Object.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class Object {

}
18 changes: 18 additions & 0 deletions scripting/engine/Reference.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@propertyWrapper
struct Reference<T> {
var uuid: String?

init(uuid: String? = nil) {
self.uuid = uuid
}

var wrappedValue: T? {
get {
guard let uuid = uuid else {
return nil
}


}
}
}
17 changes: 17 additions & 0 deletions scripting/engine/assets/Asset.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

class Asset {
var name: String
var path: String
var type: String
var data: Data?
var uuid: String

init(name: String, path: String, type: String, data: Data, uuid: String) {
self.name = name
self.path = path
self.type = type
self.uuid = uuid
self.data = data
}
}
6 changes: 6 additions & 0 deletions scripting/engine/assets/AssetManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AssetManager {
static let shared = AssetManager()

private init() {
}
}
13 changes: 13 additions & 0 deletions scripting/engine/assets/AssetPackage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class AssetPackage {
public init(path: String) {

}

public func hasAsset<T>(type: T.Type, uuid: String) -> Bool {
return false
}

public func getAsset<T>(type: T.Type, uuid: String) -> T? {
return nil
}
}
10 changes: 10 additions & 0 deletions scripting/engine/assets/AssetPackageManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AssetPackageManager {
static let shared = AssetPackageManager()

private init() {
}

lazy var packages: [AssetPackage] = {
return []
}()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@Component public struct MaterialComponent: Codable {
@Reference(type: Material.self)
public var material: String
}
3 changes: 3 additions & 0 deletions scripting/engine/ecs/builtin/components/MeshComponent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@Component public struct MeshComponent {
public var mesh: Mesh
}
2 changes: 1 addition & 1 deletion scripting/engine/math/Vector2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct Vec2<T> where T: Hashable, T: Comparable, T: Numeric {
}
}

typealias Vector2 = Vec2<Float>
public typealias Vector2 = Vec2<Float>

extension Vec2: Hashable {
public static func == (lhs: Vec2<T>, rhs: Vec2<T>) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion scripting/engine/math/Vector3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct Vec3<T> where T: Hashable, T: Comparable, T: Numeric {
}
}

typealias Vector3 = Vec3<Float>
public typealias Vector3 = Vec3<Float>

extension Vec3: Hashable {
public static func == (lhs: Vec3<T>, rhs: Vec3<T>) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion scripting/engine/math/Vector4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public struct Vec4<T> where T: Hashable, T: Comparable, T: Numeric {
}
}

typealias Vector4 = Vec4<Float>
public typealias Vector4 = Vec4<Float>

extension Vec4: Hashable {
public static func == (lhs: Vec4<T>, rhs: Vec4<T>) -> Bool {
Expand Down
52 changes: 52 additions & 0 deletions scripting/engine/rendering/Material.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Foundation

public class Material {
internal var uuid: String
public var name: String
public var shader: Shader?
public var properties: [String: Any] = [:]

init(name: String, uuid: String) {
self.name = name
self.uuid = uuid

AssetManager.shared
}

public init() {
self.name = ""
self.uuid = UUID().uuidString
}

public func load() {

}

public func setFloat(name: String, value: Float) {
properties[name] = value
}

public func setVector2(name: String, value: Vector2) {
properties[name] = value
}

public func setVector3(name: String, value: Vector3) {
properties[name] = value
}

public func setVector4(name: String, value: Vector4) {
properties[name] = value
}

public func setTexture(name: String, value: Texture) {
properties[name] = value
}

public func setInt(name: String, value: Int) {
properties[name] = value
}

public func setBool(name: String, value: Bool) {
properties[name] = value
}
}
Loading

0 comments on commit dc9859f

Please sign in to comment.