1 people like it.

kRPC example (Kerbal Space Program)

When running the kRPC Remote Procedure Call Server in a Kerbal Space Program game the kRPC C# Client can be accessed from within F# Interactive in Visual Studio. Learn more about the game here http://www.kerbalspaceprogram.com. Official kRPC C# Client documentation can be found here http://djungelorm.github.io/krpc/docs/csharp/client.html. Note: This snippet was (re-)uploaded because the original (http://fssnip.net/8qR) went lost.

Assembly references for FSI

1: 
2: 
3: 
4: 
5: 
6: 
// We can use the kRPC C# client library in F#.

#r @"mscorlib"
#r @"System.Runtime" // in my GAC
#r @"DLLs/Google.Protobuf.dll" // from https://www.nuget.org/packages/Google.Protobuf
#r @"DLLs/KRPC.Client.dll" // from kRPC 0.2.1 (in the client folder)

Connecting to the Server

1: 
let connection = new KRPC.Client.Connection (name= "FSI Example")

Interacting with the Server (version information)

1: 
2: 
open KRPC.Client.Services.KRPC
do printfn "Version: %A." (connection.KRPC().GetStatus().Version)

Interacting with the Server (name of active vessel)

1: 
2: 
3: 
4: 
open KRPC.Client.Services.SpaceCenter
let spacecenter = connection.SpaceCenter ()
let vessel = spacecenter.ActiveVessel
do printfn "Name of vessel: %A." (vessel.Name)

Streaming Data from the Server (straight)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
let refframe = vessel.Orbit.Body.ReferenceFrame

/// Calculate Kerbin altitude in meters.
/// Note: Assumes the reference frame is Kerbin.
let altOfPos (x,y,z) =
  sqrt( x*x + y*y + z*z ) - 600000.

let pos0 = vessel.Position refframe

do printfn "Altitude of position: %.0f." (altOfPos pos0)

Streaming Data from the Server (lambda)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
open System.Threading
open System.Linq.Expressions
open Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter

let positionstream: KRPC.Client.Stream<float*float*float> =
    let quotation = <@ vessel.Position refframe @>
    let body =  quotation |> QuotationToExpression
    let lambda = Expression.Lambda(body, Seq.empty)
    connection.AddStream lambda

let getAltitude () =
    positionstream.Get () |> altOfPos

let writeAltitude obj =
    do printfn "Altitude: %7.1f m." (getAltitude ())

do writeAltitude ()

let myTimer = new Timer (new TimerCallback (writeAltitude), (), 5000, 2500)
// do myTimer.Dispose ()  // dispose will kill the timer
val connection : obj

Full name: Script.connection
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val spacecenter : obj

Full name: Script.spacecenter
val vessel : obj

Full name: Script.vessel
val refframe : obj

Full name: Script.refframe
val altOfPos : x:float * y:float * z:float -> float

Full name: Script.altOfPos


 Calculate Kerbin altitude in meters.
 Note: Assumes the reference frame is Kerbin.
val x : float
val y : float
val z : float
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val pos0 : float * float * float

Full name: Script.pos0
namespace System
namespace System.Threading
namespace System.Linq
namespace System.Linq.Expressions
namespace Microsoft
namespace Microsoft.FSharp
namespace Microsoft.FSharp.Linq
namespace Microsoft.FSharp.Linq.RuntimeHelpers
module LeafExpressionConverter

from Microsoft.FSharp.Linq.RuntimeHelpers
val positionstream : obj

Full name: Script.positionstream
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val quotation : Quotations.Expr<obj>
val body : Expression
val QuotationToExpression : Quotations.Expr -> Expression

Full name: Microsoft.FSharp.Linq.RuntimeHelpers.LeafExpressionConverter.QuotationToExpression
val lambda : LambdaExpression
Multiple items
type Expression =
  member CanReduce : bool
  member NodeType : ExpressionType
  member Reduce : unit -> Expression
  member ReduceAndCheck : unit -> Expression
  member ReduceExtensions : unit -> Expression
  member ToString : unit -> string
  member Type : Type
  static member Add : left:Expression * right:Expression -> BinaryExpression + 1 overload
  static member AddAssign : left:Expression * right:Expression -> BinaryExpression + 2 overloads
  static member AddAssignChecked : left:Expression * right:Expression -> BinaryExpression + 2 overloads
  ...

Full name: System.Linq.Expressions.Expression

--------------------
type Expression<'TDelegate> =
  inherit LambdaExpression
  member Compile : unit -> 'TDelegate + 1 overload
  member Update : body:Expression * parameters:IEnumerable<ParameterExpression> -> Expression<'TDelegate>

Full name: System.Linq.Expressions.Expression<_>
Expression.Lambda(body: Expression, parameters: System.Collections.Generic.IEnumerable<ParameterExpression>) : LambdaExpression
   (+0 other overloads)
Expression.Lambda(body: Expression, [<System.ParamArray>] parameters: ParameterExpression []) : LambdaExpression
   (+0 other overloads)
Expression.Lambda<'TDelegate>(body: Expression, parameters: System.Collections.Generic.IEnumerable<ParameterExpression>) : Expression<'TDelegate>
   (+0 other overloads)
Expression.Lambda<'TDelegate>(body: Expression, [<System.ParamArray>] parameters: ParameterExpression []) : Expression<'TDelegate>
   (+0 other overloads)
Expression.Lambda(body: Expression, name: string, parameters: System.Collections.Generic.IEnumerable<ParameterExpression>) : LambdaExpression
   (+0 other overloads)
Expression.Lambda(delegateType: System.Type, body: Expression, parameters: System.Collections.Generic.IEnumerable<ParameterExpression>) : LambdaExpression
   (+0 other overloads)
Expression.Lambda(delegateType: System.Type, body: Expression, [<System.ParamArray>] parameters: ParameterExpression []) : LambdaExpression
   (+0 other overloads)
Expression.Lambda(body: Expression, tailCall: bool, parameters: System.Collections.Generic.IEnumerable<ParameterExpression>) : LambdaExpression
   (+0 other overloads)
Expression.Lambda(body: Expression, tailCall: bool, [<System.ParamArray>] parameters: ParameterExpression []) : LambdaExpression
   (+0 other overloads)
Expression.Lambda<'TDelegate>(body: Expression, name: string, parameters: System.Collections.Generic.IEnumerable<ParameterExpression>) : Expression<'TDelegate>
   (+0 other overloads)
module Seq

from Microsoft.FSharp.Collections
val empty<'T> : seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.empty
val getAltitude : unit -> float

Full name: Script.getAltitude
val writeAltitude : obj:'a -> unit

Full name: Script.writeAltitude
Multiple items
val obj : 'a

--------------------
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj
val myTimer : Timer

Full name: Script.myTimer
Multiple items
type Timer =
  inherit MarshalByRefObject
  new : callback:TimerCallback -> Timer + 4 overloads
  member Change : dueTime:int * period:int -> bool + 3 overloads
  member Dispose : unit -> unit + 1 overload

Full name: System.Threading.Timer

--------------------
Timer(callback: TimerCallback) : unit
Timer(callback: TimerCallback, state: obj, dueTime: int, period: int) : unit
Timer(callback: TimerCallback, state: obj, dueTime: System.TimeSpan, period: System.TimeSpan) : unit
Timer(callback: TimerCallback, state: obj, dueTime: uint32, period: uint32) : unit
Timer(callback: TimerCallback, state: obj, dueTime: int64, period: int64) : unit
type TimerCallback =
  delegate of obj -> unit

Full name: System.Threading.TimerCallback
Raw view New version

More information

Link:http://fssnip.net/7Pi
Posted:1 years ago
Author:Robert Nielsen
Tags: addon , example , fsi , game , ksp , mod , plugin , tutorial , krpc , client