瀏覽代碼

Rename env.cxx to linux_env.cxx.
Add osx_env.mm for loading from macOS.

Sam Jaffe 6 年之前
父節點
當前提交
5a64638b53
共有 5 個文件被更改,包括 44 次插入9 次删除
  1. 7 5
      util/gameutils.xcodeproj/project.pbxproj
  2. 1 3
      util/include/game/util/env.hpp
  3. 0 0
      util/src/files.cxx
  4. 1 1
      util/env.cxx
  5. 35 0
      util/src/osx_env.mm

+ 7 - 5
util/gameutils.xcodeproj/project.pbxproj

@@ -7,8 +7,8 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
-		CD62FCFE2291951400376440 /* env.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FCFC2291951400376440 /* env.cxx */; };
 		CD62FD04229195FF00376440 /* files.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD02229195FF00376440 /* files.cxx */; };
+		CD62FD082291988F00376440 /* osx_env.mm in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD072291988F00376440 /* osx_env.mm */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -45,9 +45,10 @@
 /* Begin PBXFileReference section */
 		CD3AC7081D2C0726002B4BB0 /* libgameutils.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libgameutils.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD62FCE622904AD500376440 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
-		CD62FCFC2291951400376440 /* env.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = env.cxx; sourceTree = "<group>"; };
+		CD62FCFC2291951400376440 /* linux_env.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = linux_env.cxx; sourceTree = "<group>"; };
 		CD62FCFF2291953700376440 /* game */ = {isa = PBXFileReference; lastKnownFileType = folder; name = game; path = include/game; sourceTree = "<group>"; };
 		CD62FD02229195FF00376440 /* files.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = files.cxx; sourceTree = "<group>"; };
+		CD62FD072291988F00376440 /* osx_env.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = osx_env.mm; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -82,10 +83,11 @@
 		CD3AC70A1D2C0726002B4BB0 /* src */ = {
 			isa = PBXGroup;
 			children = (
-				CD62FCFC2291951400376440 /* env.cxx */,
+				CD62FCFC2291951400376440 /* linux_env.cxx */,
+				CD62FD072291988F00376440 /* osx_env.mm */,
 				CD62FD02229195FF00376440 /* files.cxx */,
 			);
-			name = src;
+			path = src;
 			sourceTree = "<group>";
 		};
 		CD62FCE722904AD500376440 /* Products */ = {
@@ -223,7 +225,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				CD62FCFE2291951400376440 /* env.cxx in Sources */,
+				CD62FD082291988F00376440 /* osx_env.mm in Sources */,
 				CD62FD04229195FF00376440 /* files.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;

+ 1 - 3
util/include/game/util/env.hpp

@@ -11,7 +11,5 @@
 #include <string>
 
 namespace env {
-  std::string resource_file(std::string const & relative_path) {
-    return relative_path;
-  }
+  std::string resource_file(std::string const & relative_path);
 }

util/files.cxx → util/src/files.cxx


+ 1 - 1
util/env.cxx

@@ -1,5 +1,5 @@
 //
-//  env.cxx
+//  linux_env.cxx
 //  gameutils
 //
 //  Created by Sam Jaffe on 5/19/19.

+ 35 - 0
util/src/osx_env.mm

@@ -0,0 +1,35 @@
+//
+//  osx_env.mm
+//  gameutils
+//
+//  Created by Sam Jaffe on 5/19/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "game/util/env.hpp"
+
+#import <Cocoa/Cocoa.h>
+
+namespace {
+  NSUInteger encoding = NSUTF8StringEncoding;
+  NSString* translate(std::string const & str) {
+    return [NSString stringWithCString:str.c_str() encoding:encoding];
+  }
+}
+
+namespace env {
+  std::string resource_file(std::string const& path) {
+    size_t dir_idx = path.find_last_of("/");
+    size_t ext_idx = path.find_first_of(".");
+    std::string base = path.substr(0, dir_idx);
+    std::string name = path.substr(dir_idx + 1, ext_idx - (dir_idx + 1));
+    std::string type = path.substr(ext_idx + 1);
+    
+    NSString* url = [[NSBundle mainBundle] pathForResource:translate(name)
+                                                    ofType:translate(type)
+                                               inDirectory:translate(base)];
+    
+    char const* abs_path = [url cStringUsingEncoding:encoding];
+    return abs_path ? std::string(abs_path) : std::string();
+  }
+}